Hello world,
this is odd
the first snip of code should not be aloud ( i think)
let a = 1000f32 ;
let clos = || a*0.1f32; // clos is infered as impl Fn() -> f32t says analyzer
but when i type -which should be the same -, i get the right error
let clo2: impl Fn() -> f32 = || a*0.1f32;
compiler magic? what is the type of clos?
Remco
jofas
July 18, 2023, 1:05pm
2
RemCode:
i get the right error
You get an error, because you can't use impl Trait
in as type annotation in a let binding. Your closure does implement Fn() -> f32
though. As to why Rust analyzer displays impl Fn() -> f32
as type hint instead of the concrete type of the closure (which looks something like [closure@src/main.rs:3:16: 3:18]
), I assume it is because it is deemed more helpful by most Rust users.
Edit: I think I found the issue and PR for this feature:
opened 06:52PM - 17 May 22 UTC
closed 08:01PM - 10 Apr 23 UTC
C-feature
A-inlay-hints
I would like to see `impl Fn`, `impl FnMut`, ... as inlay hint for closures, whi… ch I think is a useful data and non trivial in first look. In current state IMO it make sense to enable this by default.
_Originally posted by @HKalbasi in https://github.com/rust-lang/rust-analyzer/issues/12263#issuecomment-1127401961_
rust-lang:master
← HKalbasi:mir
opened 04:55PM - 02 Apr 23 UTC
This PR:
* Computes closure captures and the trait it implements (Fn, FnMut or … FnOnce)
* Computes data layout of closures
* Adds support for closure MIR lowering
* Changes the closure type display from `|arg1: ty1, arg2: ty| -> ret` to `impl FnX(arg1: ty1, arg2: ty2) -> ret`
fix #12297
3 Likes
vague
July 18, 2023, 1:07pm
3
If you don't use impl Fn() -> f32
, what else can be written as an owned type here?
The trait object needs a pointer which is not the case here.
RemCode:
but when i type -which should be the same -, i get the right error
let clo2: impl Fn() -> f32 = || a*0.1f32;
The syntax is already designed:
2 Likes