File tree 1 file changed +6
-4
lines changed
src/librustc_error_codes/error_codes
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 .
2
2
3
3
Erroneous code example:
4
4
@@ -13,11 +13,13 @@ impl T for S {
13
13
14
14
// Having the trait `T` as return type is invalid because
15
15
// unboxed trait objects do not have a statically known size:
16
- fn foo() -> dyn T {
16
+ fn foo() -> dyn T { // error!
17
17
S(42)
18
18
}
19
19
```
20
20
21
+ Return types cannot be ` dyn Trait ` s as they must be ` Sized ` .
22
+
21
23
To avoid the error there are a couple of options.
22
24
23
25
If 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`]:
32
34
# }
33
35
// The compiler will select `S(usize)` as the materialized return type of this
34
36
// function, but callers will only know that the return type implements `T`.
35
- fn foo() -> impl T {
37
+ fn foo() -> impl T { // ok!
36
38
S(42)
37
39
}
38
40
```
@@ -57,7 +59,7 @@ impl T for O {
57
59
58
60
// This now returns a "trait object" and callers are only be able to access
59
61
// associated items from `T`.
60
- fn foo(x: bool) -> Box<dyn T> {
62
+ fn foo(x: bool) -> Box<dyn T> { // ok!
61
63
if x {
62
64
Box::new(S(42))
63
65
} else {
You can’t perform that action at this time.
0 commit comments