This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +6
-4
lines changed
src/librustc_error_codes/error_codes Expand file tree Collapse file tree 1 file changed +6
-4
lines changed Original file line number Diff line number Diff line change 1- Return types cannot be ` dyn Trait ` s as they must be ` Sized ` .
1+ An unboxed trait object was used as a return value .
22
33Erroneous code example:
44
@@ -13,11 +13,13 @@ impl T for S {
1313
1414// Having the trait `T` as return type is invalid because
1515// unboxed trait objects do not have a statically known size:
16- fn foo() -> dyn T {
16+ fn foo() -> dyn T { // error!
1717 S(42)
1818}
1919```
2020
21+ Return types cannot be ` dyn Trait ` s as they must be ` Sized ` .
22+
2123To avoid the error there are a couple of options.
2224
2325If there is a single type involved, you can use [ ` impl Trait ` ] :
@@ -32,7 +34,7 @@ If there is a single type involved, you can use [`impl Trait`]:
3234# }
3335// The compiler will select `S(usize)` as the materialized return type of this
3436// function, but callers will only know that the return type implements `T`.
35- fn foo() -> impl T {
37+ fn foo() -> impl T { // ok!
3638 S(42)
3739}
3840```
@@ -57,7 +59,7 @@ impl T for O {
5759
5860// This now returns a "trait object" and callers are only be able to access
5961// associated items from `T`.
60- fn foo(x: bool) -> Box<dyn T> {
62+ fn foo(x: bool) -> Box<dyn T> { // ok!
6163 if x {
6264 Box::new(S(42))
6365 } else {
You can’t perform that action at this time.
0 commit comments