我正在try 编写如下代码,其中我描述了特征中的一些代码接口,并提供了一个默认实现(本例中为模拟):
pub trait Person {
fn say_hello(&self) -> String;
fn stub() -> impl Person { // Doesn't work
MockPerson {}
}
}
pub struct MockPerson {}
impl Person for MockPerson {
fn say_hello(&self) -> String {
"hello".to_string()
}
}
我正在try 找到正确的方式来表示特征Person
有一个静态方法来返回一些有意未知的实现类型:
pub trait Person {
...
// `impl Trait` only allowed in function and inherent method return types,
// not in trait method return
fn stub() -> impl Person { ... }
}
实现这一目标的最佳方式是什么?