warning: trait objects without an explicit `dyn` are deprecated
--> file.rs:3:13
|
3 | fn foo() -> Trait {
| ^^^^^ help: use `dyn`: `dyn Trait`
|
= note: `#[warn(bare_trait_objects)]` on by default
error[E0746]: return type cannot have an unboxed trait object
--> file.rs:3:13
|
3 | fn foo() -> Trait {
| ^^^^^ doesn't have a size known at compile-time
|
help: use some type `T` that is `T: Sized` as the return type if all return paths have the same type
|
3 | fn foo() -> T {
| ^
help: use `impl Trait` as the return type if all return paths have the same type but you want to expose only the trait in the signature
|
3 | fn foo() -> impl Trait {
| ^^^^^^^^^^
help: use a boxed trait object if all return paths implement trait `Trait`
|
3 | fn foo() -> Box<dyn Trait> {
| ^^^^^^^^^^^^^^
https://play.rust-lang.org/?version=nightly&mode=debug&editi...Edit: and for some more realistic cases where the compiler can actually look at what you wrote, instead of just giving up because you used `todo!()`:
warning: trait objects without an explicit `dyn` are deprecated
--> file.rs:7:20
|
7 | fn foo(x: bool) -> Trait {
| ^^^^^ help: use `dyn`: `dyn Trait`
|
= note: `#[warn(bare_trait_objects)]` on by default
error[E0308]: `if` and `else` have incompatible types
--> file.rs:11:9
|
8 | / if x {
9 | | S
| | - expected because of this
10 | | } else {
11 | | D
| | ^ expected struct `S`, found struct `D`
12 | | }
| |_____- `if` and `else` have incompatible types
error[E0746]: return type cannot have an unboxed trait object
--> file.rs:7:20
|
7 | fn foo(x: bool) -> Trait {
| ^^^^^ doesn't have a size known at compile-time
|
= note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types>
= note: if all the returned values were of the same type you could use `impl Trait` as the return type
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
= note: you can create a new `enum` with a variant for each returned type
help: return a boxed trait object instead
|
7 | fn foo(x: bool) -> Box<dyn Trait> {
8 | if x {
9 | Box::new(S)
10| } else {
11| Box::new(D)
|
and warning: trait objects without an explicit `dyn` are deprecated
--> file.rs:7:20
|
7 | fn foo(x: bool) -> Trait {
| ^^^^^ help: use `dyn`: `dyn Trait`
|
= note: `#[warn(bare_trait_objects)]` on by default
error[E0746]: return type cannot have an unboxed trait object
--> file.rs:7:20
|
7 | fn foo(x: bool) -> Trait {
| ^^^^^ doesn't have a size known at compile-time
|
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
help: use `impl Trait` as the return type, as all return paths are of type `S`, which implements `Trait`
|
7 | fn foo(x: bool) -> impl Trait {
| ^^^^^^^^^^