Consider I have a fn that returns a String
:
fn do_things() -> String {
let res1 = match do_other_thing() {
Ok(res) => res,
Err(e) => return format!("do_other_thing failed because {}", e)
}
let res2 = match do_other_thing_2(&res1) {
Ok(res) => res,
Err(e) => return format!("do_other_thing_2 failed because {}", e)
}
format!("well done: {}", res2)
}
Here I cannot use Result
as a return type because fn signature is dictated by the framework.
So I want to collapse Result<String, String>
to String
on error.
Can I somehow make "?"
sign return String
instead of Result
?
Like this:
fn do_things() -> String {
let res1 = do_other_thing().map_err(|e| format!("do_other_thing failed because {}", e))?;
let res2 = do_other_thing_2(&res1).map_err(|e| format!("do_other_thing_2 failed because {}", e))?;
format!("well done: {}", res2)
}