Skip to content

Instantly share code, notes, and snippets.

@henryobiaraije
Created May 15, 2023 21:46
Show Gist options
  • Save henryobiaraije/10470f1e2dd85bfc150d500efc628236 to your computer and use it in GitHub Desktop.
Save henryobiaraije/10470f1e2dd85bfc150d500efc628236 to your computer and use it in GitHub Desktop.
A function in Rust language to divide one number by another.
// 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