Result Ok and Err
Write a function divide that takes two f64 numbers and returns Result<f64, String>. If the second number is 0.0, return an Err with the message "cannot divide by zero". Otherwise return Ok with the result. Test it with 10.0 / 2.0 and 10.0 / 0.0.
Expected output:
text
10 / 2 = 5 10 / 0 = Error: cannot divide by zero
Hints:
- Return
Err(String::from("cannot divide by zero"))for division by zero - Return
Ok(a / b)for valid division - Use
matchinmainto handle both cases
rust
fn main() {
}