How to return Option<Self>
from a trait
Notice how you can restrict Self
the same way you can restrict T in a parameterized type.
trait X where Self: Sized {
fn parent(&self) -> Option<Self>;
}
#[derive(Clone)]
struct Bar;
impl X for Bar {
fn parent(&self) -> Option<Bar> {
return Some(self.clone());
}
}
#[test]
fn foo() {
let x = Bar;
let y = Bar.parent();
}