reference - 在创建别名可变借用之后但在使用之前使用不可变借用实际上很危险吗?

标签 reference rust borrow-checker mutability borrowing

此 MCVE:

struct A {
    b: B,
}

struct B {
    c: i32,
}

fn f(_a: &A) {}

fn g(_b: &mut B) {}

fn main() {
    let mut foo = A { b: B { c: 2 } };
    let bar = &mut foo.b;
    f(&foo);
    g(bar);
}

导致以下错误:

error[E0502]: cannot borrow `foo` as immutable because it is also borrowed as mutable
  --> src/main.rs:16:7
   |
15 |     let bar = &mut foo.b;
   |               ---------- mutable borrow occurs here
16 |     f(&foo);
   |       ^^^^ immutable borrow occurs here
17 |     g(bar);
   |       --- mutable borrow later used here

我理解为什么同时拥有可变和不可变借用是危险的,但由于 f 不返回任何内容,因此在使用 bar 时,引用&foo 不再使用。这段代码实际上是危险的,还是编译器的限制?如果是这样,编写这段代码的惯用方法是什么?我需要使用手机吗?

最佳答案

Is this code actually dangerous, or is it a limitation of the compiler?

嗯...它并不危险,因为它无法编译。


如果编译成功了?

让我们想象一下,您不使用 rustc,而是使用 mrustc ,一个 Rust-to-C 编译器,它假设代码是正确的并且不执行借用检查。

将代码正确再现到 C:

struct B { int c; };

struct A { struct B b; };

void f(struct A const* a) {}

void g(struct B* restrict b) {}

int main(int argc, char** argv) {
    struct A foo = { { 2 } };
    struct B* restrict bar = &foo.b;
    f(&foo);
    g(bar);
}

请注意 restrict 限定符的存在,C 语言相当于 &mut,它向编译器表明该指针没有别名。 restrict (强调我的):

During each execution of a block in which a restricted pointer P is declared (typically each execution of a function body in which P is a function parameter), if some object that is accessible through P (directly or indirectly) is modified, by any means, then all accesses to that object (both reads and writes) in that block must occur through P (directly or indirectly), otherwise the behavior is undefined.

我邀请您检查该链接,还有其他几种情况会导致未定义的行为。

我不清楚这是否会成为这里的问题,毕竟您没有执行任何修改。

不过,根据 C++ 的经验,我建议避免模棱两可的情况:如果你无法证明它是正确的,那么它就很危险

关于reference - 在创建别名可变借用之后但在使用之前使用不可变借用实际上很危险吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53990135/

相关文章:

javascript - 为什么我的对象引用不为空?

c++ - 如果一个引用一旦被初始化为一个对象,它就不能被改变,为什么会这样呢?

ubuntu - SDL2 - undefined reference - Ubuntu

compiler-errors - 为什么将同一文件中的相同结构包含到多个包装箱中会导致类型不匹配错误?

types - 强类型枚举作为 rust 中的联合 : How to determine type of value and retrieve it; How to do "constructors"

rust - 由于需求冲突,无法推断出自动强制的适当生命周期

rust - 如何将特征作为结构的自有字段?

c++ - 存储右值引用 : should this work?

rust - rust “Types differ in mutability”

rust - 将集合移出上下文