gpt4 book ai didi

rust - move 后改变结构的字段

转载 作者:行者123 更新时间:2023-12-01 09:41:45 24 4
gpt4 key购买 nike

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

考虑代码:

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 (!)
}

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

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

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

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

最佳答案

现在禁止这样做。

过去是允许的。然而,这是旧的借用检查器中的一个错误,并且在引入新的借用检查器 (NLL) 时先是警告然后是错误。

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

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`

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

另请注意,这是 2018 版的一个错误,持续时间较长(可能自该版本创建以来)。

另见:

关于rust - move 后改变结构的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59447283/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com