-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-closuresArea: Closures (`|…| { … }`)Area: Closures (`|…| { … }`)A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.
Description
It's clear that the closure could implement Fn
, but for some reason this code does not compile.
fn constrain<F: FnMut()>(f: F) -> F {
f
}
pub fn foo() -> impl Fn() { // works if FnMut
constrain(|| {})
}
Error
error[E0277]: expected a `std::ops::Fn<()>` closure, found `[closure@src/lib.rs:6:15: 6:20]`
--> src/lib.rs:5:17
|
5 | pub fn foo() -> impl Fn() { // works if FnMut
| ^^^^^^^^^ expected an `Fn<()>` closure, found `[closure@src/lib.rs:6:15: 6:20]`
|
= help: the trait `std::ops::Fn<()>` is not implemented for `[closure@src/lib.rs:6:15: 6:20]`
= note: wrap the `[closure@src/lib.rs:6:15: 6:20]` in a closure with no arguments: `|| { /* code */ }
= note: the return type of a function must have a statically known size
This bit someone at our company who's relatively new to rust while trying to return a std::iter::Map<.., impl Fn(..)->..>
. The Iterator::map
function appears to have the same effect as constrain
.
Metadata
Metadata
Assignees
Labels
A-closuresArea: Closures (`|…| { … }`)Area: Closures (`|…| { … }`)A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.