To iterate over a two-dimensional vector in Rust, you can use nested loops.
In this example,
for
loops to iterate over each element of the two-dimensional vector.fn main() {
// Step 1: Iterate over a numeric two-dimensional vector
let matrix = vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]];
// Step 2: Nested loops to print each element
for row in &matrix {
for &num in row {
println!("{}", num);
}
}
}
123456789
In this example,
for
loops to iterate over each element of the two-dimensional vector.fn main() {
// Step 1: Iterate over a string two-dimensional vector
let matrix = vec![vec!["a", "b", "c"], vec!["d", "e", "f"], vec!["g", "h", "i"]];
// Step 2: Nested loops to print each element
for row in &matrix {
for &ch in row {
println!("{}", ch);
}
}
}
abcdefghi
In this tutorial, we learned How to Iterate over a Two Dimensional Vector in Rust language with well detailed examples.