From Rust doc:
Result<T, E> could have one of two outcomes:
So I am assuming it cannot have both? I am trying to do something in a function that may potentially result in a recoverable error. I want to both do recovery and return the error if possible. However, not sure what is the idiomatic way to do that in Rust, or can/should it be done at all.
For reference, I have the following code:
extern crate anyhow;
fn divide(a:f64, b:f64) -> Result<f64, anyhow::Error>{
if b==0.0 {
return Err(anyhow::anyhow!("Divide by zero"));
}
Ok(a/b)
}
fn main() {
let r=divide(3.0,0.0);
match r {
Ok(d) => {
println!("Result: {}",d);
}
Err(e) => {
println!("Error: {}",e);
}
}
}
Can I get a default result of 1.0
even if the Divide by zero occur?
Thank you.