【发布时间】:2018-08-26 15:33:27
【问题描述】:
我正在尝试编写一个派生 PartialEq 的枚举,其中包含一个手动执行的特征对象。我使用解决方案here 来强制Trait 的实现者编写一个相等方法。这无法编译:
trait Trait {
fn partial_eq(&self, rhs: &Box<Trait>) -> bool;
}
impl PartialEq for Box<Trait> {
fn eq(&self, rhs: &Box<Trait>) -> bool {
self.partial_eq(rhs)
}
}
#[derive(PartialEq)]
enum Enum {
Trait(Box<Trait>),
}
error[E0507]: cannot move out of borrowed content
--> src/main.rs:13:11
|
13 | Trait(Box<Trait>),
| ^^^^^^^^^^^ cannot move out of borrowed content
这仅在我手动 impl PartialEq for Enum 时编译。为什么会这样?
【问题讨论】:
-
您可能想在您的
partial_eq中使用&Trait;&Box有不需要的双重间接。
标签: rust ownership ownership-semantics