I want to store some closures in a HashMap
. I'm sure these closures will not capture anything. I tried two methods, shown in the following example:
use std::collections::HashMap;
enum A {
B,
C,
}
fn donotwork() {
let g: HashMap<u8, fn(A) -> A> = HashMap::from([(0, |x: A| x)]);
let h: HashMap<u8, Box<dyn (Fn(A) -> A) + Sync>> = HashMap::from([(0, Box::new(|x: A| x))]);
}
(Playground link here)
(I used HashMap
just to make sure the environment here is similar enough to the real scenario. The Fn
can be replaced by FnOnce
and FnMut
, with similar results.)
Both won't compile.
error[E0308]: mismatched types
--> src/lib.rs:9:38
|
9 | let g: HashMap<u8, fn(A) -> A> = HashMap::from([(0, |x: A| x)]);
| ----------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `HashMap<u8, fn(A) -> A>`, found `HashMap<{integer}, ...>`
| |
| expected due to this
|
= note: expected struct `HashMap<u8, fn(A) -> A>`
found struct `HashMap<{integer}, [closure@src/lib.rs:9:57: 9:63]>`
error[E0308]: mismatched types
--> src/lib.rs:10:56
|
10 | let h: HashMap<u8, Box<dyn (Fn(A) -> A) + Sync>> = HashMap::from([(0, Box::new(|x: A| x))]);
| ----------------------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `HashMap<u8, Box<...>>`, found `HashMap<{integer}, Box<...>>`
| |
| expected due to this
|
= note: expected struct `HashMap<u8, Box<dyn Fn(A) -> A + Sync>>`
found struct `HashMap<{integer}, Box<[closure@src/lib.rs:10:84: 10:90]>>`
I don't understand what these error mean. Doesn't closures implement the Fn
trait? Why is the closure with (strange) type [closure@...]
? How can I fix the issue?
Thanks.