To iterate over a Two Dimensional Array in Rust, you can use nested loops.
In this example,
matrix
.fn main() {
let matrix = [[0, 1, 2], [1, 2, 3], [2, 3, 4]];
for row in &matrix {
for &element in row {
print!("{} ", element);
}
println!();
}
}
0 1 2 1 2 3 2 3 4
In this example,
matrix
.fn main() {
let matrix = [['a', 'b'], ['c', 'd']];
for row in &matrix {
for &element in row {
print!("{} ", element);
}
println!();
}
}
a b c d
In this tutorial, we learned How to Iterate over a Two Dimensional Array in Rust language with well detailed examples.