let mut a = [1, 2, 3, 4, 5];
let mut window = &a[1..4];
for element in window.iter() {
println!("{}", element);
*element = 0;
}
如何通过切片a
将原始数组0
中的中间值设置为window
?
答案 0 :(得分:4)
在您的确切情况下,如果您不尝试制作重叠切片,则只需创建&mut
切片:
let mut a = [1, 2, 3, 4, 5];
let window = &mut a[1..4];
for element in window.iter_mut() {
println!("{}", element);
*element = 0;
}