我了解何时可以将特征制成特征对象的规则,但我不明白为什么存在这些规则。
例如:
trait Resource {
const RESOURCE_ID: u64;
}
trait ResourceStatic {
fn static_id() -> u64;
}
trait ResourceInstance {
fn resource_id(&self) -> u64;
}
struct MyResource {}
impl Resource for MyResource {
const RESOURCE_ID: u64 = 123;
}
impl ResourceStatic for MyResource {
fn static_id() -> u64 {
123
}
}
impl ResourceInstance for MyResource {
fn resource_id(&self) -> u64 {
123
}
}
在我看来,这三个特征基本上都封装了相同的功能。那么为什么不允许这些:
let _: Box<dyn Resource> = Box::new(MyResource{});
let _: Box<dyn ResourceStatic> = Box::new(MyResource{});
但这是?
let _: Box<dyn ResourceInstance> = Box::new(MyResource{});
有人可以解释一下引擎盖下发生了什么,以免它看起来很随意吗?