[英]Why I can't borrow immutable reference of a mutable reference
let mut v = vec![1, 2, 3];
let r = &mut v;
let rr = &r;
r.push(4);
println!("{:?}, {:?}", r, rr);
Gives error:给出错误:
error[E0502]: cannot borrow `*r` as mutable because it is also borrowed as immutable
|
86 | let rr = &r;
| -- immutable borrow occurs here
87 | r.push(4);
| ^^^^^^^^^ mutable borrow occurs here
88 | println!("{:?}, {:?}", r, rr);
| -- immutable borrow later used here
I understand the concept of immutable reference can not be borrowed for *r
, if there is already a borrowed mutable reference of *r
.我理解不可变引用的概念不能为
*r
借用,如果已经有*r
的借用可变引用。 However, in this case I am trying to borrow immutable reference of r
, not of *r
.但是,在这种情况下,我试图借用
r
的不可变引用,而不是*r
的引用。
A reference is just a variable, like any other variable.引用只是一个变量,就像任何其他变量一样。
If you replace the variable r
with an integer, you should see why this isn't possible:如果将变量
r
替换为 integer,您应该明白为什么这是不可能的:
fn main() {
let mut r = 10;
let rr = &r;
r += 2;
println!("{:?}, {:?}", r, rr);
}
error[E0506]: cannot assign to `r` because it is borrowed
--> src/main.rs:4:5
|
3 | let rr = &r;
| -- borrow of `r` occurs here
4 | r += 2;
| ^^^^^^ assignment to borrowed `r` occurs here
5 | println!("{:?}, {:?}", r, rr);
| -- borrow later used here
Any mutable variable automatically becomes immutable while an immutable reference to it exists.当存在对它的不可变引用时,任何可变变量都会自动变为不可变。
If that weren't the case, the owner of the rr
variable couldn't rely on the fact that the content of rr
never changes while he has the reference, which is one of the main points of immutable references.如果不是这种情况,则
rr
变量的所有者不能相信rr
的内容在他拥有引用时永远不会改变这一事实,这是不可变引用的要点之一。 It doesn't just mean that you can't modify the variable, but you can also rely on the fact that no one else modifies it while you hold the reference.这不仅仅意味着您不能修改变量,而且您还可以依赖这样一个事实,即当您持有引用时没有其他人修改它。
So the pure existence of rr
makes r
temporarily immutable.所以
rr
的纯粹存在使得r
暂时不可变。 r
will automatically become mutable again once rr
stops existing, but as you println.(/*.,*/, rr)
later, you force rr
to stay alive until then and you force r
to be immutable.一旦
rr
停止存在, r
将再次自动变为可变的,但是当您稍后println.(/*.,*/, rr)
时,您强制rr
在此之前保持活动状态,并强制r
不可变。 That's why the modification fails.这就是修改失败的原因。
The fact that in your code r
is a reference doesn't change anything about this language mechanism.在您的代码中
r
是一个引用这一事实并没有改变这种语言机制的任何内容。 In your code, if this wouldn't error, the value rr
references would change while rr
exists.在您的代码中,如果这不会出错,则
rr
引用的值会在rr
存在时发生变化。 And again, the fact that rr
is an immutable reference guarantees that the content that rr
is referencing won't change.同样,
rr
是不可变引用这一事实保证了rr
引用的内容不会改变。
Remark: The way I worded 'mutability' in this answer is somewhat incomplete, as 'interior mutability' exists, but it was easier to describe it this way.备注:我在这个答案中表达“可变性”的方式有些不完整,因为存在“内部可变性”,但这样描述起来更容易。 In reality, the words 'mutability' and 'immutability' should be replaced with 'exclusive access' and 'non-exclusive access'.
实际上,“可变性”和“不变性”这两个词应该替换为“独占访问”和“非独占访问”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.