当对该变量的可变引用可以时,为什么不能使用可变变量? [英] Why can't a mutable variable be used when a mutable reference to that variable can?

查看:29
本文介绍了当对该变量的可变引用可以时,为什么不能使用可变变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可变字符串变量和一个绑定到可变字符串变量的可变引用的不可变变量.

I have a mutable string variable, and an immutable variable bound to a mutable reference to the mutable string variable.

let mut string = String::from("test");
let variable: &mut String = &mut string;
variable.push_str(" test");
string.push_str(" test");

这失败了:

error[E0499]: cannot borrow `string` as mutable more than once at a time
 --> src/main.rs:5:5
  |
3 |     let variable: &mut String = &mut string;
  |                                      ------ first mutable borrow occurs here
4 |     variable.push_str(" test");
5 |     string.push_str(" test");
  |     ^^^^^^ second mutable borrow occurs here
6 | }
  | - first borrow ends here

  1. 如果第二个变量不可变,为什么我可以调用 push_str?
  2. 为什么我可以对第二个变量而不是第一个变量调用 push_str?

推荐答案

您收到此错误是因为可变借用是独占的:

You're getting this error because mutable borrowing is exclusive:

let mut string = String::from("test")
let variable = &mut string;

这里创建了一个对变量的可变引用;因为可变引用意味着独占访问,所以现在不可能访问原始变量,否则将违反别名保证.

Here you create a mutable reference to a variable; because mutable references imply exclusive access, it is now impossible to access the original variable, because otherwise you would be violating aliasing guarantees.

考虑一下:

let mut string = String::from("test");
{
    let variable = &mut string;
    variable.push_str(" test");
}
string.push_str(" test");

此代码将按预期编译和工作,因为在再次访问原始变量之前,可变引用已超出范围.

This code will compile and work as intended, because the mutable reference goes out of scope before the original variable is accessed again.

您可以阅读有关此内容的更多信息 在 Rust 书中(参见 本书第二版的链接).

You can read more about this in the Rust book (see this link for the second edition of the book).

至于为什么可以在非 mut 变量上调用 mutating 方法,嗯,这可能仅仅是因为 push_str() 方法通过 &mut;如果你已经有 &mut 则直接使用它,但如果你没有,那么 Rust 会自动尝试为你创建一个,如果变量不是 <代码>mut:

As for why you can call the mutating method on a non-mut variable, well, it is possible simply because the push_str() method accepts its receiver by &mut; if you already have &mut then it is used directly, but if you don't have one, then Rust will automatically try to create one for you, which is not possible if the variable is not mut:

let mut string = String::from("test");

string.push_str("test");
// equivalent to:
String::push_str(&mut string, "test");  // won't work if `string` is not `mut`

let variable = &mut string;
variable.push_str("test");
// [almost] equivalent to:
String::push_str(variable, "test");  // works because `variable` is already `&mut`

我在上面的例子中写了几乎",因为在这种情况下,还有另一个步骤称为重新借用,它基本上确保在这次调用之后可以再次使用可变引用而不是移到函数调用中,但它没有这个答案真的很重要.

I wrote "almost" in the example above because in this case there is another step called reborrowing which basically ensures that the mutable reference can be used again after this call instead of being moved into the function call, but it doesn't really matter for this answer.

这篇关于当对该变量的可变引用可以时,为什么不能使用可变变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆