Created
May 15, 2023 21:46
-
-
Save henryobiaraije/10470f1e2dd85bfc150d500efc628236 to your computer and use it in GitHub Desktop.
A function in Rust language to divide one number by another.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Full tutorial : https://daily-dose-of-rust-language.pereere.com/2023/05/15/a-simple-example-of-error-handling-and-testing-in-rust/ | |
fn divide(a: f64, b: f64) -> Result<f64, String> { | |
if 0.0 == b { | |
return Result::Err("Hey, you can't divide by zero".to_string()); // Checking if b is equal to zero. If true, return an Err variant of the Result enum with an error message. | |
} | |
return Result::Ok(a / b); // If the condition is false, return an Ok variant of the Result enum with the result of dividing a by b. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment