Menu
Coddy logo textTech

Recap - If Else

Part of the Fundamentals section of Coddy's Rust journey — lesson 27 of 75.

challenge icon

Challenge

Beginner

You are given a code which gets as input two numbers n1 and n2 and a single char string op.

Note: we will learn in next lessons how to get input from the user, currently just don't touch the three first lines.

 

The possible values for op are "+", "-", "/" and "*"

Your task is to set the variable result based on the conditions:

  • if op is "+", set result with n1 + n2.
  • if op is "-", set result with n1 - n2.
  • if op is "/", set result with n1 / n2.
  • if op is "*", set result with n1 * n2.

Try it yourself

use std::io;

fn main() {
    let mut n1_input = String::new();
    let mut n2_input = String::new();
    let mut op_input = String::new();
    
    io::stdin().read_line(&mut n1_input).unwrap();
    io::stdin().read_line(&mut n2_input).unwrap();
    io::stdin().read_line(&mut op_input).unwrap();
    
    let n1: f64 = n1_input.trim().parse().unwrap();
    let n2: f64 = n2_input.trim().parse().unwrap();
    let op = op_input.trim();
    
    // Write your code below, use n1, n2 and op
    let result: f64 = 0.0;
    
    
    
    println!("{}", result);
}

All lessons in Fundamentals