通过数组切片变换数组

时间:2015-03-27 19:24:16

标签: rust

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

1 个答案:

答案 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;
}