我有一个特征对象,我想知道它指向的具体对象,但我不知道如何获取具体对象。
我想要的是如下内容:
trait MyClonable {
/** copy from another MyClonable */
fn my_clone_from(&mut self, other: &Self)->Result<(), MyError>;
}
impl MyClonable for SomeType {
fn my_clone_from(&mut self, other: &MyClonable)->Result<(), MyError> {...}
}
这样我就可以说:
let mut new_thing = SomeType::new();
new_thing.my_clone_from(&old_thing)?;
然后new_thing
将包含 的某种副本old_thing
,除非old_thing
是意外类型,在这种情况下它应该返回错误。
但是 Rust 不会让我Option<&SomeType>
从MyClonable
.