我正在努力学习物体安全的基本知识.如果我有这个密码
struct S {
x: i32,
}
trait Trait: Sized {
fn f(&self) -> i32
where
Self: Sized;
}
fn object_safety_dynamic(x: Trait) {}
我收到
error[E0038]: the trait `Trait` cannot be made into an object
--> src/lib.rs:11:29
|
5 | trait Trait: Sized {
| ----- ----- ...because it requires `Self: Sized`
| |
| this trait cannot be made into an object...
...
11 | fn object_safety_dynamic(x: Trait) {}
| ^^^^^ the trait `Trait` cannot be made into an object
When I add or remove : Sized
as the supertrait or as f
's bound, 我收到 slightly different error messages.
谁能解释一下:
-
为什么这个例子不起作用?第Trait Objects章规定:
那么,是什么让方法对象安全呢?每种方法都必须要求
这不是满足了吗?
-
Trait: Sized
和where Self: Sized
之间有什么区别?(好吧,是的,一个继承了特征,另一个是参数绑定的,但是从Rust的特征对象的Angular 来看? -
我需要做的preferred个改变是什么?
如果重要的话,我用rustc 1.19.0-nightly (01951a61a 2017-05-20)
.
针对固定尺寸的 comments .
trait TraitB {
fn f(&self) -> i32
where
Self: Sized;
fn g<T>(&self, t: T) -> i32
where
Self: Sized;
}