-Return types cannot be `dyn Trait`s as they must be `Sized`.
+An unboxed trait object was used as a return value.
Erroneous code example:
// Having the trait `T` as return type is invalid because
// unboxed trait objects do not have a statically known size:
-fn foo() -> dyn T {
+fn foo() -> dyn T { // error!
S(42)
}
```
+Return types cannot be `dyn Trait`s as they must be `Sized`.
+
To avoid the error there are a couple of options.
If there is a single type involved, you can use [`impl Trait`]:
# }
// The compiler will select `S(usize)` as the materialized return type of this
// function, but callers will only know that the return type implements `T`.
-fn foo() -> impl T {
+fn foo() -> impl T { // ok!
S(42)
}
```
// This now returns a "trait object" and callers are only be able to access
// associated items from `T`.
-fn foo(x: bool) -> Box<dyn T> {
+fn foo(x: bool) -> Box<dyn T> { // ok!
if x {
Box::new(S(42))
} else {