There have been many proposals about ways we could get rid of Ok(()) at the end of functions that return a Result<(), Error>:
fn do_stuff() -> Result<(), Error> {
let foo = foo()?;
bar(foo)?;
let baz = baz(None)?;
foo_bar_baz(foo, baz)?;
Ok(())
}
Recently, I came across a piece of code that did this:
fn do_stuff() -> Result<(), Error> {
// ...
Ok(unsafe {
let unsafe_thing = make_unsafe_thing();
do_some_unsafe_thing(unsafe_thing);
})
}
Using the implicit () that the unsafe block evaluates to to create the Ok(()) return value without having to add a separate line, which seemed a lot nicer (unfortunately clippy complains). So I realized that if we had the hypothetical one-line functions that have been floated around a lot for try, async, etc., we could remove the need for Ok(()) while still explicitly writing Ok:
// one line functions: fn foo() -> Foo = foo();
fn do_stuff() -> Result<(), Error> = Ok({
let foo = foo()?;
bar(foo)?;
let baz = baz(None)?;
foo_bar_baz(foo, baz)?;
})
This would work with non-unit return values as well:
fn do_stuff() -> Result<Foo, Error> = Ok({
foo()
})
There has also been a lot of support for using the implicit Ok wrapping of try blocks for this:
fn do_stuff() -> Result<Foo, Error> = try {
foo()
}
I think I prefer the Ok({ version because I don't like implicit wrapping in general, but curious what other peoples thoughts are?