The following piece of code raises type error

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::Stringrustc(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.

You’ve changed the type of the variable when you are doing parse since you’re reusing the same variable name. In the loop, the second time through you’re passing a u32 to read_line. Try using a different variable name for the u32 version of the temperature variable.

Also in general please format code by putting it in code blocks. It makes it much easier to read

```
Code goes here
```
2 Likes

Several issues:

  • The Err tuple variant has 1 field, so you need an Err(_) pattern.
  • The variables choice: u32 and temp: u32 are local to the two loops, they are not visible in main after the loops.
  • In choice == 1 you are comparing the variable choice: String with an integer, which is not valid.
  • u32 is not a suitable type to represent temperature. What about negative values?

Here is a simple refactor. Notice that it doesn't address the last issue, which is left as an exercise. Try running it an entering choice = 2, temp = 31.

Edit: also, Celsius is spelt Celsius, not Celcius.

1 Like

Aah, thanks for all the help. And celcius has just become a habit, sorry :sweat_smile::sweat_smile:. And also extremely sorry for not formatting the code.

Souvik flutter dev

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.