The Rust compiler is usually able to infer the type of an expression that is returned from a closure:
fn main() {
let a_closure = |num|{
num+1.0
};
println!("{}", a_closure(1.0));
}
But the compiler is unable to infer the type when I define the same closure using a return statement:
fn main() {
let a_closure = |num|{
return num+1.0
};
println!("{}", a_closure(1.0));
}
/*
error[E0308]: mismatched types
--> src/main.rs:3:9
|
3 | return num+1.0
| ^^^^^^^^^^^^^^ expected `()`, found `f64`
*/
I'm surprised that Rust can't infer the type here: is it possible to use a return statement in a closure without preventing the compiler from inferring its return type?