简体   繁体   English

为什么我不能借用可变引用的不可变引用

[英]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.

相关问题 可变借款后的不变参考 - Immutable reference after mutable borrow 为什么我不能通过可变值的可变引用的不可变引用来更新值? - Why can't I update a value through an immutable reference of a mutable reference of a mutable value? 不能将数据作为“&”引用作为可变借用 - Can't borrow data as a '&' reference as mutable 为什么我可以使用可变变量使其生命周期与不可变引用重叠,但我不能以相同的方式使用可变引用? - Why can I use a mutable variable such that its lifetime overlaps with an immutable reference, but I can't use a mutable reference in the same way? 为什么 Rust 不能对不可变变量进行可变借用? - Why can't Rust do mutable borrow on an immutable varaiable? 为什么你可以借用一个可变引用并仍然使用两者? - Why can you borrow a mutable reference and still use both? 您不能借用对只读值的可变引用 - You can’t borrow a mutable reference to a read-only value 为什么 Rust 不能在类型构造函数中强制可变引用到不可变引用? - Why Rust can't coerce mutable reference to immutable reference in a type constructor? 当对象上的特征没有时,为什么引用上的特征会引发“不能借用为可变的,因为它也被借用为不可变的”? - Why does a trait on a reference raise "cannot borrow as mutable because it is also borrowed as immutable" when a trait on an object does not? 当范围内仍然有不可变的借用字符串切片引用时,为什么编译器在此可变借用上没有出错? - Why did compiler not error on this mutable borrow when there is an immutable borrowed string slice reference still in scope?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM