There are several problems in your code.
First of all, you cannot use the Fn* traits directly in stable Rust. This includes 1) the use of notation with angle brackets and 2) the implementation of these features. However, it is possible to enable the function flag for both of these things in unstable Rust.
Secondly, if you use angle brackets for signs of closure, you need to use tuples for the arguments, even if there is only one argument:
FnOnce<(A,)>
Thirdly, the error message is that instead of FnOnce<(T, U), Output=V> should write FnOnce(T, U) -> V This means that it means using parentheses. I agree that this post is misleading because you cannot implement Fn when it is written like this because of the related types. I assume that an error in the implementation of Fn types should take precedence over this error.
Fourth, you won’t be able to do what you want (the memoizing function supported by the hash map) when you use &'a MemoedFun<A, R> because you need a mutable pointer to update the map. You need to implement FnOnce for &'a mut MemoedFun<A, R> :
impl<'a, A: Eq + Hash, R> FnOnce<(A,)> for &'a mut MemoedFun<A, R> { type Output = &'a R; extern "rust-call" fn call_once(self, (arg,): (A,)) -> &'a R { if self.map.contains_key(&arg) { &self.map[&arg] } else { let r = (self.fun)(&arg); self.map.entry(arg).or_insert(r) } } }
And finally, the resulting code that you need to write to use this memoizer is not very good. You cannot use functional syntax for your “function” for any reason, so you will need to use call_once() directly:
fn computer(x: &i32) -> i32 { println!("Computing for {}", x); -*x } let mut f = memoize(computer); println!("f(10): {}", (&mut f).call_once((10,))); println!("f(10): {}", (&mut f).call_once((10,))); println!("f(42): {}", (&mut f).call_once((42,)));
(try here )
There is a reason that Fn* features in manual mode are not stabilized.