Menu
Coddy logo textTech

Recap - Safe Division Function

Part of the Logic & Flow section of Coddy's Rust journey — lesson 48 of 66.

challenge icon

Challenge

Easy

You will receive two inputs. The first input is a number representing the dividend, and the second input is a number representing the divisor. Create a function safe_divide that takes two f64 parameters and returns a Result<f64, &'static str>. The function should return Err("Cannot divide by zero") when the divisor is zero, and Ok with the division result otherwise. Use a match expression to handle the result and print the appropriate output.

Requirements:

  • Read the first input (dividend) and trim whitespace
  • Parse the first input to f64
  • Read the second input (divisor) and trim whitespace
  • Parse the second input to f64
  • Create a function safe_divide that takes two f64 parameters and returns Result<f64, &'static str>
  • Inside the function, check if the divisor is 0.0:
    • If yes, return Err("Cannot divide by zero")
    • If no, return Ok(dividend / divisor)
  • Call the function with the two parsed numbers
  • Use a match expression to handle the Result
  • In the Ok(value) arm, print: Division result: [value]
  • In the Err(error) arm, print: Error: [error]

Input:

  • First line: A number representing the dividend (e.g., 20.0)
  • Second line: A number representing the divisor (e.g., 4.0)

Output:

  • If division is successful: Division result: [result]
  • If division by zero: Error: Cannot divide by zero

Try it yourself

use std::io;

// TODO: Create the safe_divide function here
// fn safe_divide(dividend: f64, divisor: f64) -> Result<f64, &'static str> {
//     Your code here
// }

fn main() {
    // Read the first input (dividend)
    let mut dividend_input = String::new();
    io::stdin().read_line(&mut dividend_input).expect("Failed to read line");
    let dividend: f64 = dividend_input.trim().parse().expect("Invalid number");
    
    // Read the second input (divisor)
    let mut divisor_input = String::new();
    io::stdin().read_line(&mut divisor_input).expect("Failed to read line");
    let divisor: f64 = divisor_input.trim().parse().expect("Invalid number");
    
    // TODO: Call safe_divide and use match to handle the Result
    // Print the appropriate output based on the result
}

All lessons in Logic & Flow