I was trying to create an example for dispatch tables. the first way, with a temporary mutable and calling insert works, but when I try to create table2 I get mismatched types error with some further details:
every function that has defined has its own sepcific type (e.g add has fn(i32,i32) -> i32 {add} ),multiply has it as well,I think you can use add as fn(i32,i32) -> i32 to solve this problem.
There's a type inference variable between the annotation and the trait parameter, so it isn't coercing the function items and closures in your tuples. This works (not saying it's good).
let table2: HashMap<_, _> = <[(char, fn(i32, i32) -> i32); 4]>::into([
('+', add),
('*', multiply),
('-', |x, y| x - y),
('/', |x, y| x / y),
]);
To explain what I meant better, perhaps, this doesn't work:
// Type inference variable v
let table2 = <HashMap<char, fn(i32, i32) -> i32> as From<_>>::from([
('+', add),
('*', multiply),
('-', |x, y| x - y),
('/', |x, y| x / y),
]);