Closed
Description
Code
let a = |x: dyn Fn()| x();
Current output
--> src/lib.rs:14:17
|
14 | let a = |x: dyn Fn()| x();
| ^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn Fn() + 'static)`
help: you can use `impl Trait` as the argument type
|
14 | let a = |x: impl Fn()| x();
| ~~~~
help: function arguments must have a statically known size, borrowed types always have a known size
|
14 | let a = |x: &dyn Fn()| x();
| +
Desired output
help: function arguments must have a statically known size, borrowed types always have a known size
|
14 | let a = |x: &dyn Fn()| x();
| +
Rationale and extra context
We start with some obviously invalid code, rustc
makes two suggestions - pass &dyn Trait
by reference - this works and use impl Trait
which doesn't work.
error[E0562]: `impl Trait` is not allowed in closure parameters
--> src/lib.rs:15:17
|
15 | let b = |x: impl Fn()| x();
| ^^^^^^^^^
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods
For more information about this error, try `rustc --explain E0562`.
Usually following compiler suggestions you get closer to having working code, but here this leads to a dead end.
Other cases
Rust Version
rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: x86_64-unknown-linux-gnu
release: 1.85.0
LLVM version: 19.1.7
Anything else?
Nightly makes a similar suggestion, also broken.