I started reading the Rust guide on closures. From the guide:
That is because in Rust each closure has its own unique type. So, not only do closures with different signatures have different types, but different closures with the same signature have different types, as well.
Is there a way to explicitly write the type signature of a closure? Is there any compiler flag that expands the type of inferred closure?
No. The real type of a closure is only known to the compiler, and it's not actually that useful to be able to know the concrete type of a given closure. You can specify certain "shapes" that a closure must fit, however:
In this case, we say that
call_it
accepts any type that implements the traitFn
with one argument of typeu8
and a return type ofu8
. Many closures and free functions can implement that trait however.As of Rust 1.26.0, you can also use the
impl Trait
syntax to accept or return a closure (or any other trait):