...

/

Solution Review 2: Make a Calculator

Solution Review 2: Make a Calculator

This lesson gives a detailed solution review to the challenge in the previous lesson.

We'll cover the following...

Solution :

Press + to interact
fn test(a: i32, operator: char ,b: i32) {
match operator {
'+' => {
println!("{}", a + b);
},
'-' => {
println!("{}", a - b);
},
'*' => {
println!("{}", a * b);
},
'/' => {
if b == 0{
println!("Division by 0 is undefined");
}
else {
println!("{}", a / b);
}
},
'%' => {
if b == 0{
println!("Mod 0 is undefined");
}
else {
println!("{}", a % b);
}
},
_ => println!("{}", "invalid operator"),
}
}
fn main(){
print!("3 + 2: ");
test(3,'+',2);
print!("3 - 2: ");
test(3,'-',2);
print!("3 * 2: ");
test(3,'*',2);
print!("3 / 2: ");
test(3,'/',2);
print!("3 % 2: ");
test(3,'%',2);
print!("3 ( 2: ");
test(3,'(',2);
print!("3 ( 0: ");
test(3, '/', 0)
}

Explanation

  • match construct

    A match construct is ...