This is my very first independant rust project that I made by myself. I am very curious on the things i could've done better and how i couldve wrote everything more efficiently / better looking.
use std::io;
fn main() {
let _operators: [char; 4] = ['+', '-', '*', '/'];
let num1: f32;
let num2: f32;
let result: f32;
let operator: String;
loop {
let mut input = String::new();
println!("enter num1:");
io::stdin().read_line(&mut input).expect("failed to read line.");
match input.trim().parse() {
Ok(n) => {
num1 = n; break;
},
Err(_) => continue,
}
};
loop {
let mut input = String::new();
println!("enter num2:");
io::stdin().read_line(&mut input).expect("failed to read line.");
match input.trim().parse() {
Ok(n) => {
num2 = n; break;
},
Err(_) => continue,
}
};
loop {
let mut input = String::new();
println!("enter an operator (+, -, *, /):");
io::stdin().read_line(&mut input).expect("failed to read line.");
match input.trim() {
"+" | "-" | "*" | "/" => {
operator = input.trim().to_string();
break;
},
_ => continue
}
}
match operator.as_str() {
"+" => result = num1 + num2,
"-" => result = num1 - num2,
"*" => result = num1 * num2,
"/" => result = num1 / num2,
_ => return
}
println!("{num1} + {num2} = {result}");
}```
thanks!