我有个特质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)