为什么在使用 `dyn Trait` 的类型别名时会出现大小错误?

Why do I get a size error when using a type alias of `dyn Trait`?

此函数编译:

fn edit<S: AsRef<str>>(w: S) {}

如果我为通用参数键入别名:

type Word = dyn AsRef<str>;

fn edit(w: Word) {}

我收到一个错误:

error[E0277]: the size for values of type `(dyn std::convert::AsRef<str> + 'static)` cannot be known at compilation time
 --> src/lib.rs:3:9
  |
3 | fn edit(w: Word) {}
  |         ^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `(dyn std::convert::AsRef<str> + 'static)`
  = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
  = note: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature

为什么会这样?

所有函数都需要在编译时知道其参数的大小。但是,您使用的 w 其大小无法在编译时确定。为了能够执行动态调度,您需要使用特征对象。您可以通过两种方式完成此操作。

您可以使用 Box:

传递指针
fn edit(w: Box<Word>)

或参考:

fn edit(w: &Word)

您的另一个选择是完全避免使用特征对象。您可以改为使 edit 成为通用方法,如下所示:

fn edit(w: impl AsRef<str>)

fn edit<W: AsRef<str>>(w: W)

使用这种方法,编译器会执行所谓的 "monomorphization",它会在编译时确定要为泛型参数传入哪些类型,并为每个调用生成多个非泛型版本与那些特定类型。 What is monomorphisation with context to C++?.

中很好地解释了单态化