为什么 for 循环不需要可变迭代器?

Why does a for loop not require a mutable iterator?

如果我想手动使用迭代器,它必须是可变的:

let test = vec![1, 2, 3];
let mut test_mut = test.iter();
while let Some(val) = test_mut.next() {
    println!("{:?}", val);
}

但我可以愉快地使用 for 循环使用它,即使它是不可变的。

let test = vec![1, 2, 3];
let test_imm = test.iter();
for val in test_imm {
    println!("{:?}", val);
}

认为这是可行的,因为test_imm被移动到for循环的块中,所以test_imm不能再被外部块使用并且(从外部块的角度来看)在 for 循环之前是不可变的,然后它就不可访问了,所以没关系。

是吗?还有更多要解释的吗?

完全正确。因为它被移动到 for 循环中,for 循环现在拥有它并且可以用它做任何它想做的事情,包括 "making it" 可变的。考虑这个类似的例子,尽管 xs 是不可变的,但我们似乎正在改变它,但实际上是因为我们正在移动它,所以新所有者可以随意使用它,包括重新绑定它作为可变的:

let xs: Vec<i32> = vec![1, 2, 3];

fn append(v: Vec<i32>, x: i32) -> Vec<i32> {
    let mut my_v = v;
    my_v.push(x);
    my_v
}

let appended = append(xs, 4);

playground

请注意,可以使用 mut 参数便利语法使函数更短:

fn append(mut v: Vec<i32>, x: i32) -> Vec<i32> {
    v.push(x);
    v
}

这或多或少得到了解释in the iter module's documentation