我发现这篇博客文章非常清楚地解释了这个特性:https://medium.com/digitalfrontiers/rust-dynamic-dispatching-deep-dive-236a5896e49b
相关摘录:
代码语言:javascript运行复制struct Service
backend: Vec
}
...
let mut backends = Vec::new();
backends.push(TypeA);
backends.push(TypeB); // <---- Type error herevs
代码语言:javascript运行复制struct Service{
backends: Vec
}
...
let mut backends = Vec::new();
backends.push( Box::new(PositiveBackend{}) as Box
backends.push( Box::new(NegativeBackend{}) as Box