具有 Sized 超特征的特征仍然存在错误 "std::marker::Sized is not satisfied"
A trait with a Sized supertrait still has the error "std::marker::Sized is not satisfied"
我有以下代码:
use std::collections::HashMap;
trait T: Sized {}
struct A;
impl T for A {}
fn main() {
let h: HashMap<String, T>;
}
但是编译器抱怨:
error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
--> src\main.rs:10:12
|
10 | let h: HashMap<String, T>;
| ^^^^^^^^^^^^^^^^^^ `T` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= note: required by `std::collections::HashMap`
error[E0038]: the trait `T` cannot be made into an object
--> src\main.rs:10:12
|
10 | let h: HashMap<String, T>;
| ^^^^^^^^^^^^^^^^^^ the trait `T` cannot be made into an object
|
= note: the trait cannot require that `Self : Sized`
我不明白错误消息,因为我将我的特征 T
标记为 Sized
。我错过了什么吗?
because I've marked my trait T
as Sized
不,你没有。您说过任何实现 T
的类型 必须是 Sized
。 trait 本身仍未调整大小。您要么需要特征对象(例如 Box<T>
),要么需要某种通用对象(在这种情况下您不能这样做)。
我有以下代码:
use std::collections::HashMap;
trait T: Sized {}
struct A;
impl T for A {}
fn main() {
let h: HashMap<String, T>;
}
但是编译器抱怨:
error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
--> src\main.rs:10:12
|
10 | let h: HashMap<String, T>;
| ^^^^^^^^^^^^^^^^^^ `T` does not have a constant size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= note: required by `std::collections::HashMap`
error[E0038]: the trait `T` cannot be made into an object
--> src\main.rs:10:12
|
10 | let h: HashMap<String, T>;
| ^^^^^^^^^^^^^^^^^^ the trait `T` cannot be made into an object
|
= note: the trait cannot require that `Self : Sized`
我不明白错误消息,因为我将我的特征 T
标记为 Sized
。我错过了什么吗?
because I've marked my trait
T
asSized
不,你没有。您说过任何实现 T
的类型 必须是 Sized
。 trait 本身仍未调整大小。您要么需要特征对象(例如 Box<T>
),要么需要某种通用对象(在这种情况下您不能这样做)。