【发布时间】:2020-06-13 13:43:08
【问题描述】:
我正在尝试更改枚举的命名属性,但出现此错误。
cannot move out of a mutable referencerustc(E0507)
parser.rs(323, 36): data moved here
parser.rs(323, 36): move occurs because `statements` has type `std::vec::Vec<std::boxed::Box<ast::ast::StatementType>>`, which does not implement the `Copy` trait
我看到我们可以使用 match 语句更改枚举的命名道具。但是我不明白为什么会发生移动,因为我是在借用枚举本身。代码如下:
match &mut block {
StatementType::Block { mut statements, .. } => {
statements = block_statements;
},
_ => panic!()
};
return block;
我也尝试过 mem::swap 但仍然是同样的错误:
match &mut block {
StatementType::Block { mut statements, .. } => {
// statements = block_statements;
std::mem::swap(&mut statements, &mut block_statements);
},
_ => panic!()
};
return block;
但是当我这样做时:
std::mem::swap(&mut *statements, &mut *block_statements);
错误变为:
the size for values of type `[std::boxed::Box<ast::ast::StatementType>]` cannot be known at compilation time
doesn't have a size known at compile-time
类型是:
- StatementType 是派生克隆的枚举
- Block 是 StatementType 的可变变量
- 块的
statements是Vec<Box<StatementType>>的变量 -
block_statements是Vec<Box<StatementType>>的另一个变量
请不要说它的发生是因为语句的类型是向量:请提供解决方案,因为我也可以阅读错误消息。
【问题讨论】:
-
你能提供一个游乐场的例子吗?