简体   繁体   English

为什么编译器不抱怨迭代器移动到for循环是不可变的?

[英]Why does the compiler not complain that an iterator moved to a for loop is immutable?

I am reading the second edition of the Rust Book and I found the following sample in the iterators section: 我正在阅读Rust Book的第二版,我在迭代器部分找到了以下示例:

let v1 = vec![1, 2, 3];
let v1_iter = v1.iter();    
for val in v1_iter {
    println!("Got: {}", val);
}

Why does the compiler not complain that v1_iter is immutable? 为什么编译器不抱怨v1_iter是不可变的? The book says the for loop took ownership of v1_iter and made it mutable behind the scenes, but can you convert an immutable variable to mutable? 该书说for循环取得了v1_iter所有权,并使其在幕后变得可变,但是你可以将一个不可变变量转换为可变吗?

The book says the for loop took ownership of v1_iter and made it mutable behind the scenes, 该书说for循环取得了v1_iter的所有权,并使其在幕后变得可变,

Exactly, and one can make an even simpler example: 确切地说,人们可以做一个更简单的例子:

let v = vec![1,2,3];
let mut x = v;
x.push(0);

Note that v and x are separate variable bindings: for as long as the variable v retained our 3-element vector, the contract of the variable was that the vector will not be mutated. 请注意, vx是单独的变量绑定:只要变量v保留了我们的3元素向量,变量的约定就是向量不会被变异。 However, the vector was moved to x , which declares that mutability is acceptable. 但是,向量被移动到x ,这表明可变性是可接受的。 The same applies to function calls: 这同样适用于函数调用:

fn foo(mut x: Vec<i32>) {
    x.push(0);
}

let v = vec![1,2,3];
foo(v);

This is safe because only one of the variables owns the vector at any point of its lifetime. 这是安全的,因为只有一个变量在其生命周期的任何一点拥有向量。 Once v was moved to x , v can no longer be used. v移动到x ,将无法再使用v Likewise, in your code, v1_iter can no longer be used after the for loop. 同样,在您的代码中,在for循环之后不能再使用v1_iter

but can you convert an immutable variable to mutable? 但是你可以将不可变变量转换为可变吗?

Both snippets work because the value was moved to a new variable declared as mut . 两个片段都有效,因为该值已移至声明为mut的新变量。 However, once a variable is declared as immutable (or mutable), that variable stays so for all of its lifetime, and that cannot be changed. 但是,一旦变量被声明为不可变(或可变),该变量在其所有生命周期内都保持不变,并且无法更改。 So the answer is no, but ownership semantics enable moving values across variables with different mutability guarantees. 所以答案是否定的,但所有权语义允许在具有不同可变性保证的变量之间移动值。

See also: 也可以看看:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 Rust 编译器会在我用新值替换它时抱怨我使用了移动的值? - Why does the Rust compiler complain that I use a moved value when I've replaced it with a new value? 为什么在这个 function 中移动了一个迭代器? - Why is an iterator moved in this function? 为什么接受Box的功能 <MyType> 抱怨当一个接受自我的功能起作用时价值被转移了吗? - Why does a function that accepts a Box<MyType> complain of a value being moved when a function that accepts self works? 移动 object 后,为什么 Rust 编译器不重用堆栈上的 memory? - Why does the Rust compiler not reuse the memory on the stack after an object is moved? 为什么编译器错误抱怨多个可变引用而不是悬挂引用? - Why does the compiler error complain about multiple mutable references not dangling reference? 为什么for循环不需要可变迭代器? - Why does a for loop not require a mutable iterator? 为什么 Rust 编译器在它应该是不可变的地方接受可变值? - Why does the Rust compiler accept a mutable value where it should've been immutable? 为什么在将变量移动到 scope 后 Rust 编译器错误“不能作为不可变借用,因为它也作为可变借用”? - Why does the Rust compiler error with "cannot borrow as immutable because it is also borrowed as mutable" after moving the variable into a scope? 在向迭代器借为可变后,在循环内借为不可变 - Borrow as immutable inside the loop after borrowed as mutable to the iterator 为什么这不算作不可改变的借款? - Why does this not count as an immutable borrow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM