I create a HashMap which maps strings to functions of type Vec<Expression> -> Expression
, where Expression
is a type I have defined. The code in question is:
let functions: HashMap<_, _> = vec!(("+", Box::new(plus))).into_iter().collect();
If I let Rust infer the type for me, as in the code above, it compiles and runs fine, as in the code above. However, if I try to specify the type, it doesn't compile:
let functions: HashMap<&str, Box<Fn(Vec<Expression>) -> Expression>> =
vec!(("+", Box::new(plus))).into_iter().collect();
The compiler error message isn't very helpful:
let functions: HashMap<&str, Box<Fn(Vec<Expression>) -> Expression>> = vec!(("+", Box::new(plus))).into_iter().collect();
^^^^^^^ a collection of type `std::collections::HashMap<&str, std::boxed::Box<std::ops::Fn(std::vec::Vec<Expression>) -> Expression>>` cannot be built from an iterator over elements of type `(&str, std::boxed::Box<fn(std::vec::Vec<Expression>) -> Expression {plus}>)`
What is the actual type of this HashMap
?