只有对象安全(object-safe)的 trait 可以实现为特征对象。这里有一些复杂的规则来实现 trait 的对象安全,但在实践中,只有两个相关的规则。如果一个 trait 中定义的所有方法都符合以下规则,则该 trait 是对象安全的: 返回值不是 Self 没有泛型类型的参数 Self 关键字是我们在 trait 与方法上的实现的别称,trait
clone::Clone` cannot be made into an object --> src/lib.rs:2:5 | 2 | pub components: Vec<Box<dyn Clone>>, | ^^^ the trait `std::clone::Clone` cannot be made into an object | = note: the trait cannot require that `Self : Sized` 这意味着不能以这种方式使用此 trait 作为 ...
error[E0038]: the trait `Clone` cannot be made into an object --> src/lib.rs:2:29 | 2 | pub components: Vec<Box<dyn Clone>>, | ^^^ `Clone` cannot be made into an object | = note: the trait cannot be made into an object because it requires `Self: Sized` = note: for a...
如果一个trait里有不接收self的静态方法,那他就不是object safe的,例如,如果我们的Hello里有 trait Hello { fn hello(&self); fn weird(); } 那么,再想用&dyn Hello就是不可以的了,编译器会提示 the traitHellocannot be made into an object… because associated functionweirdhas noselfparameter 同时,编...
为了实现rust_gui所期望拥有的行为,定义一个Drawtrait,其包含名为draw的方法。接着可以定义一个存放trait 对象(trait object) 的 vector。trait 对象指向一个实现了我们指定 trait 的类型实例。我们通过指定某些指针,比如&引用或Box<T>智能指针,接着指定相关的 trait(第十九章动态大小类型部分会介绍 trait 对象必须...
clone::Clone` cannot be made into an object --> src/lib.rs:2:5 | 2 | pub components: Vec<Box<dyn Clone>>, | ^^^ the trait `std::clone::Clone` cannot be made into an object | = note: the trait cannot require that `Self : Sized` 这意味着不能以这种方式使用此 trait 作为 ...
为了实现 gui 所期望拥有的行为,定义一个 Draw trait,其包含名为 draw 的方法。接着可以定义一个存放 trait 对象(trait object)的 vector。trait 对象指向一个实现了我们指定 trait 的类型实例。我们通过指定某些指针,比如 & 引用或 Box 智能指针,接着指定相关的 trait(第十九章 「动态大小类型」 部分会介绍 ...
rust 无法复制Vec〈Box< Trait>>,因为Trait无法变成对象解决方案在于结合到目前为止的注解中的建议--在...
例如,带有返回Self方法clone的trait Clone就不能够使用。 pubstructScreen{pubcomponents:Vec<Box<dyn Clone>>,^^^the trait `std::clone::Clone` cannot be madeintoanobject} 1. 2. 3. 4.
我如何解决不能为实现我的trait的所有事物定义Ord,也不能使用Ord的trait对象的问题?由于你拥有trait,...