In the code below, the lines using the string "not ok" do not compile, they get a Rust compiler error "Mismatched types".
I do not understand why the compiler rejects these constructs, as both variables f3 and f4 define closures that return a struct X when called.
Coming from a Scala background, I would appreciate input from Rust experts on why these calls to f3 and f4 fail.
Many thanks !
JL
=================
struct X {
some_string: String
}
impl X {
pub fn consume_closure<T>(s: String, f: fn(&String) -> T) -> T {
f(&s)
}
fn f1(s: &String) -> X { X { some_string: s.clone() } }
fn test() {
let f2 = |s: &String|-> X { X { some_string: s.clone() } };
let f3: fn() -> X = || { Self::f1(&"Hello".to_string()) };
let f4: fn(&String) -> X = |s: &String| { Self::f1(s) };
let x: X = Self::consume_closure("ok".to_string(), Self::f1);
let x: X = Self::consume_closure("ok".to_string(), f2);
let x: X = Self::consume_closure("ok".to_string(), |s: &String| { X { some_string: s.clone() } });
let x: X = Self::consume_closure("not ok".to_string(), |s: &String| { f3() });
let x: X = Self::consume_closure("not ok".to_string(), |s: &String| { f4(s) });
let x: X = Self::consume_closure("ok".to_string(), |s: &String| { Self::f1(s) });
}
}