How to get matrix size in Rust

1 Answer

0 votes
fn main() {
    let matrix = vec![
        vec![2, 0, 5, 9],
        vec![0, 4, 0, 3],
        vec![0, 8, 0, 1],
    ];

    // Get the number of rows
    let rows = matrix.len();

    // Get the number of columns
    let columns = matrix[0].len();

    let total_cells = rows * columns;

    println!("Matrix size: {} rows x {} columns", rows, columns);
    println!("Total Cells: {}", total_cells);
}



/*
run:

Matrix size: 3 rows x 4 columns
Total Cells: 12

*/

 



answered Oct 2, 2025 by avibootz
...