How to print an array using for loop in Rust

3 Answers

0 votes
fn main() {
    let arr:[i32; 6] = [3, 19, 21, 0, 189, 7];
    
    for index in 0..6 {
        println!("index: {} & value: {}",index, arr[index]);
    }
}






/*
run:

index: 0 & value: 3
index: 1 & value: 19
index: 2 & value: 21
index: 3 & value: 0
index: 4 & value: 189
index: 5 & value: 7

*/

 



answered Oct 25, 2022 by avibootz
0 votes
fn main() {
    let arr:[i32; 6] = [150, 19, 21, 0, 189, 99];
    
    for index in 0..arr.len() {
        println!("index: {} & value: {}",index, arr[index]);
    }
}






/*
run:

index: 0 & value: 150
index: 1 & value: 19
index: 2 & value: 21
index: 3 & value: 0
index: 4 & value: 189
index: 5 & value: 99

*/

 



answered Oct 25, 2022 by avibootz
0 votes
fn main() {
    let arr:[i32; 6] = [150, 19, 21, 0, 189, 99];
    
    for val in arr.iter() {
        println!("{}", val);
    }
}






/*
run:

150
19
21
0
189
99

*/

 



answered Oct 25, 2022 by avibootz

Related questions

1 answer 159 views
1 answer 157 views
1 answer 173 views
1 answer 165 views
1 answer 114 views
2 answers 148 views
...