移动后改变结构的字段

Ran*_*ala 3 alias move ownership-semantics rust

我对以下行为感到困惑:有人可以解释发生了什么吗?

考虑代码:

struct Point {
    cx : u32,
}
fn main() {
    let mut p1 = Point { cx: 100 };
    let     p2 = p1; 
    p1.cx      = 5000;
    // println!("p1.x = {}", p1.cx); // disallowed as p1.cx is "moved" ... ok
    println!("p2.x = {}", p2.cx); // ==> prints 100 (!)
}
Run Code Online (Sandbox Code Playgroud)

具体来说,我感到困惑的是:

  1. 更新p1.cx允许即使举动已经发生,
  2. 返回的值p2.x实际上不是更新后的 5000,而是旧的100.

我期待新值,因为没有复制特征(因此移动),所以期待只有一个单元格,其更新值 ( 5000) 应该被打印出来。

但是,我一定错过了一些东西。有小费吗?提前致谢!

mca*_*ton 7

这是现在被禁止的。

以前是允许的。然而,这是旧借用检查器中的错误,并且在引入新借用检查器 (NLL) 时已发出警告,然后是错误。

例如,对于rustc 1.39.02015 版,您会收到以下警告:

warning[E0382]: assign to part of moved value: `p1`
 --> a.rs:8:5
  |
6 |     let mut p1 = Point { cx: 100 };
  |         ------ move occurs because `p1` has type `Point`, which does not implement the `Copy` trait
7 |     let p2 = p1;
  |              -- value moved here
8 |     p1.cx = 5000;
  |     ^^^^^^^^^^^^ value partially assigned here after move
  |
  = warning: this error has been downgraded to a warning for backwards compatibility with previous releases
  = warning: this represents potential undefined behavior in your code and this warning will become a hard error in the future
  = note: for more information, try `rustc --explain E0729`
Run Code Online (Sandbox Code Playgroud)

rustc 1.40.0 把它变成了一个错误:

error[E0382]: assign to part of moved value: `p1`
 --> src/main.rs:7:5
  |
5 |     let mut p1 = Point { cx: 100 };
  |         ------ move occurs because `p1` has type `Point`, which does not implement the `Copy` trait
6 |     let p2 = p1;
  |              -- value moved here
7 |     p1.cx = 5000;
  |     ^^^^^^^^^^^^ value partially assigned here after move

error: aborting due to previous error
Run Code Online (Sandbox Code Playgroud)

另请注意,这是 2018 版在较长时间内(可能自该版创建以来)的错误。

也可以看看: