Trait std::marker::Sized1.0.0[][src]

pub trait Sized { }
Expand description

在编译时已知常量大小的类型。

所有类型参数的隐含边界均为 Sized。如果不合适,可以使用特殊语法 ?Sized 删除此绑定。

struct Foo<T>(T);
struct Bar<T: ?Sized>(T);

// struct FooUse(Foo<[i32]>); // 错误: 没有为 [i32] 实现大小调整
struct BarUse(Bar<[i32]>); // OK
Run

一个例外是 trait 的隐式 Self 类型。 trait 没有隐式 Sized 绑定,因为它与 trait 对象 不兼容,根据定义,trait 需要与所有可能的实现者一起使用,因此可以为任意大小。

尽管 Rust 允许您将 Sized 绑定到 trait,但是以后您将无法使用它来形成 trait 对象:

trait Foo { }
trait Bar: Sized { }

struct Impl;
impl Foo for Impl { }
impl Bar for Impl { }

let x: &dyn Foo = &Impl;    // OK
// let y: &dyn Bar = &Impl; // 错误: 无法将 trait `Bar` 创建成对象
Run

Implementors