rust - 为什么编译器报告部分移动错误而不移动?
问题描述
在下面的程序中,为什么编译器错误提到部分移动而不是仅仅移动?从 x 到 z 的部分移动是如何发生的?如果 &mut T 没有实现 Copy 特征,它应该被移动而不是部分移动?
fn main() {
let mut y = 2;
let x = Some(&mut y);
match x {
None => (),
Some(z) => {
*z = 3;
println!("{}", z)},
};
println!("{:?}",x);
}
以下是操场上的错误
Compiling playground v0.0.1 (/playground)
error[E0382]: borrow of partially moved value: `x`
--> src/main.rs:10:21
|
6 | Some(z) => {
| - value partially moved here
...
10 | println!("{:?}",x);
| ^ value borrowed here after partial move
|
= note: partial move occurs because value has type `&mut i32`, which does not implement the `Copy` trait
help: borrow this field in the pattern to avoid moving `x.0`
|
6 | Some(ref z) => {
| ^^^
For more information about this error, try `rustc --explain E0382`.
error: could not compile `playground` due to previous error
解决方案
推荐阅读
- ios - 使用 Combine 在一天中的特定时间运行代码
- aws-lambda - 在 amazon lex 中管理对话上下文
- session - yii2中用户不活动后自动重定向到登录页面
- javascript - JQuery 无法为主题定制器正确切换类
- c++ - 在调试模式下抛出 C++“deque iterator not dereferencable”异常
- spring - Spring webclient 消费应用程序/pdf
- git - Jenkins 无法连接 GIT 存储库:权限被拒绝(公钥、gssapi-keyex、gssapi-with-mic、密码)
- amazon-web-services - Amazon Athena ALTER TABLE ADD PARTITION 查询给出缺失列错误
- css - 给元素排序的 SCSS 函数
- r - TextInput 没有在闪亮的 R 中给出结果