diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2020-08-05 14:14:21 +0200 |
|---|---|---|
| committer | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2020-08-05 14:14:21 +0200 |
| commit | cd4633917d4dd446f9f37efbf750bbd4ed2b46d6 (patch) | |
| tree | 9dbe5e9db1ab489c51c132cbcd3b67c7adc744c8 /src/librustc_error_codes/error_codes | |
| parent | 3a92b9987abd01c4b7e59c870e85beb9dd4d4aa2 (diff) | |
| download | rust-cd4633917d4dd446f9f37efbf750bbd4ed2b46d6.tar.gz rust-cd4633917d4dd446f9f37efbf750bbd4ed2b46d6.zip | |
Clean up E0746 explanation
Diffstat (limited to 'src/librustc_error_codes/error_codes')
| -rw-r--r-- | src/librustc_error_codes/error_codes/E0746.md | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/src/librustc_error_codes/error_codes/E0746.md b/src/librustc_error_codes/error_codes/E0746.md index 305667e58f8..90755d47f67 100644 --- a/src/librustc_error_codes/error_codes/E0746.md +++ b/src/librustc_error_codes/error_codes/E0746.md @@ -1,4 +1,4 @@ -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: @@ -13,11 +13,13 @@ impl T for S { // 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`]: @@ -32,7 +34,7 @@ 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) } ``` @@ -57,7 +59,7 @@ impl T for O { // 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 { |
