相关疑难解决方法(0)

编译器强迫我执行特征方法,但是我的类型从不满足方法上的`Self`特征

我有个特质Foo。如果那些实现者实现了另一个特征(Clone在本示例中),我想强迫实现者定义一个方法。我的想法(游乐场):

trait Foo {
    // Note: in my real application, the trait has other methods as well,
    // so I can't simply add `Clone` as super trait
    fn foo(&self) 
    where 
        Self: Clone;
}

struct NoClone;
impl Foo for NoClone {}
Run Code Online (Sandbox Code Playgroud)

可悲的是,这导致:

error[E0046]: not all trait items implemented, missing: `foo`
 --> src/lib.rs:8:1
  |
2 | /     fn foo(&self) 
3 | |     where 
4 | |         Self: Clone;
  | |____________________- `foo` from trait
... …
Run Code Online (Sandbox Code Playgroud)

traits rust

10
推荐指数
1
解决办法
211
查看次数

标签 统计

rust ×1

traits ×1