Hi,
I am a beginner and wrote a code to convert temperature from celsius to fahrenheit.
Code:-
use std::io;
fn main() {
//converting temperature from fahrenheit to celsius.
let mut temp = String::new();
let mut choice = String::new();
println!("This program converts temperature from celcius to fahrenheit");
println!("To convert temperature from celcius to fahreheit press 1.");
println!("To convert temperature from fahrenheit to celcius press 2.");
println!("Enter your choice: ");
loop {
io::stdin()
.read_line(&mut choice)
.expect("Unable to read line!");
println!("Enter temperature");
let choice: u32 = match choice.trim().parse() {
Ok(num) => num,
Err(_) => {
println! {"Invalid input!"};
continue;
}
};
}
loop {
io::stdin()
.read_line(&mut temp)
.expect("Unable to read line!");
let temp: u32 = match temp.trim().parse() {
Ok(num) => num,
Err(_) => {
println!("Invalid input!");
continue;
}
};
}
// The equalto (==) operator causes the problem.
if choice == 1 {
println!("{}", cel_to_fah(temp));
} else if choice == 2 {
println!("{}", fah_to_cel(temp));
} else {
println!("Invalid choice, aborting!")
}
}
fn cel_to_fah(temp: u32) -> u32 {
(temp * 9 / 5) + 32
}
fn fah_to_cel(temp: u32) -> u32 {
(temp - 32) * 5 / 9
}
And it raised the error:- mismatched types
expected u32
, found struct std::string::String
rustc(E0308)
main.rs(36, 35): expected u32
, found struct std::string::String
I don't know why the error is raised and how to correct the code. Please help. Thank you.