I have functions that return an Option
or a Result
:
fn get_my_result() -> Result<(), Box<Error>> {
lots_of_things()?;
Ok(()) // Could this be omitted?
}
fn get_my_option() -> Option<&'static str> {
if some_condition {
return Some("x");
}
if another_condition {
return Some("y");
}
None // Could this be omitted as well?
}
Currently, neither Ok(())
or None
are allowed to be omitted, as shown in the examples above. Is there a reason for that? Is it possible for this to be changed in the future?
You cannot omit this in Rust. A proposal was made to allow a
()
→Result<(), _>
coercion rule, but it was massively downvoted and then refused.A comment explains well why it is a bad idea:
When I have a lot of
Ok(())
in my code, I create a small helper function to make the code prettier: