1、arr.iter()方法;创造一个不可修改值的循环器;
let mut arr = vec![0;13];
for value in arr.iter(){
//value += 1; //value值不可修改
println!("value = {}", value);
}
2、iter_mut()方法;创造一个可修改值的循环器;
let mut arr = vec![0;13];
for value in arr.iter(){
value += 1; //value值可修改
println!("value = {}", value);
}
3、元组模式,arr.iter().enumerate();创造一个不可修改值的循环器,
let mut arr = vec![0;13];
for (index, value) in arr.iter().enumerate(){ //index是下标,value是值
println!("index = {}, value = {}", index, value);
}
for (index, value) in arr.iter_mut().enumerate(){ //可修改值的循环器
*value = 1;
println!("index = {}, value = {}", index, value);
}
4、下标模式
let mut arr = vec![0;13];
for i in 0..arr.len(){
println!("arr value = {}", arr[i]);
}
5、take(num)限制前num条数据
let mut arr = vec![0;13];
for (index,value) in arr.iter().take(5).enumerate(){
println!("index = {}, value = {}", index, value);
}