我想收集对 struct 的更改,并同时应用它们.
enum SomeEnum {
Foo,
Bar,
}
struct SomeStruct {
attrib: SomeEnum,
next_attrib: Option<SomeEnum>,
}
impl SomeStruct {
pub fn apply_changes(&mut self) {
if let Some(se) = self.next_attrib {
self.attrib = se;
}
self.next_attrib = None;
}
}
这将产生以下编译器错误:
error[E0507]: cannot move out of borrowed content --> src/lib.rs:13:27 | 13 | if let Some(se) = self.next_attrib { | -- ^^^^ cannot move out of borrowed content | | | hint: to prevent move, use `ref se` or `ref mut se`
我找到了Get an enum field from a struct: cannot move out of borrowed content,并在我的枚举定义中添加了#[derive(Clone, Copy)]
.
这可能有效,但我对(隐式地)使用复制感到不舒服,因为这通常也会发生在更大的数据类型上.
实际所有者永远不会移出 struct .
有没有其他方法可以做到这一点,而不向enum的所有用户公开Copy
/Clone
特征?