就像主题中的,为什么Rust阻止了多个可变引用?我已经读过rust-book中的章节,我知道当我们有多线程代码时,我们可以避免数据竞争,但让我们看看下面的代码:
fn main() {
let mut x1 = String::from("hello");
let r1 = &mut x1;
let r2 = &mut x1;
r1.insert(0, 'w');
}此代码不是同时运行的,因此不存在数据竞争的可能性。更重要的是,当我创建新线程时,我想在新线程中使用来自父线程的变量,我必须移动它,这样只有新线程才是父变量的所有者。
我能看到的唯一原因是,程序员在成长过程中可能会沉浸在代码中。我们有多个地方可以修改一段数据,即使代码不是并行运行的,我们也会得到一些错误。
发布于 2019-10-13 23:28:47
Rust同时阻止两个可变引用以防止数据竞争的事实是一个常见的误解。这只是其中一个原因。通过防止两个可变引用,可以轻松地在类型上保留不变量,并让编译器强制不违反不变量。
以下面这段C++代码为例:
#include <vector>
int main() {
std::vector<int> foo = { 1, 2, 3 };
for (auto& e: foo) {
if (e % 2 == 0) {
foo.push_back(e+1);
}
}
return 0;
}这是不安全的,因为您不能在迭代时改变向量。改变向量可能会重新分配其内部缓冲区,这会使所有引用无效。在C++中,这是一个UB。在Python、Java或C# (可能还有大多数其他语言)中,您会得到一个运行时异常。
但是,Rust可以在编译时防止此类问题:
fn main() {
let mut foo = vec![1, 2, 3];
for e in foo {
if e % 2 == 0 {
foo.push(e+1);
}
}
}给出一个错误:
error[E0382]: borrow of moved value: `foo`
--> src/main.rs:6:13
|
2 | let mut foo = vec![1, 2, 3];
| ------- move occurs because `foo` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait
3 |
4 | for e in foo {
| ---
| |
| value moved here
| help: consider borrowing to avoid moving into the for loop: `&foo`
5 | if e % 2 == 0 {
6 | foo.push(e+1);
| ^^^ value borrowed here after movehttps://stackoverflow.com/questions/58364807
复制相似问题