What is the effect of writing the value of the option when returning the closure?
let value = Some(0);
let closure = if value.is_some() {
|value: i32| false
} else {
|value: i32| true
};
The code above does not produce a compilation error However, the code below has a compilation error What's the reason?
let closure = if let Some(option_value) = value {
|value: i32| option_value == value
} else {
|value: i32| true
};
They're all different types
`if` and `else` have incompatible types
no two closures, even if identical, have the same type
consider boxing your closure and/or using it as a trait object
Box is a solution. But I don't know the difference yet
let closure = if let Some(option_value) = value {
Box::new(move |value: i32| option_value == value)
} else {
Box::new(|value: i32| true)
};