Exerpad
🔥0
0 XP
Lv.1 Beginner
Log In

Safe Calculator

Write a function safe_divide that takes two i32 values and returns Result<i32, String>. It should return an error if dividing by zero. Use it to compute 100 / 4 and 100 / 0.

Expected output:

text
100 / 4 = 25
100 / 0 = Error: division by zero

Hints:

  • Check if b == 0 first
  • Return Err(String::from("division by zero")) for the error case
  • Return Ok(a / b) for success
rust
fn main() {
}