I tried to make a temperature converter from Fahrenheit units to Celsius units on Rust

use std::io;

fn main() {
    let mut celsius = String::new();
    io::stdin()
        .read_line(&mut celsius)
        .expect("Falied to read line");
    let farenheit = (celsius * 9 / 5) + 32;
}

Returns the error: cannot multiply {integer} to std::string::String

 1
Author: saintech, 2020-11-11

1 answers

Full modified code below, using the functions str::parse::<T>() (documentation) and str::trim() (documentation):

Try it online!

use std::io;

fn main() {
    let mut celsius = String::new();
    io::stdin()
        .read_line(&mut celsius)
        .expect("Falied to read line");
    let ncelsius = celsius.trim().parse::<i32>().expect("Failed parsing int");
    let farenheit = (ncelsius * 9 / 5) + 32;
    println!("{}", farenheit);
}

Entrance:

20

Exit:

68
 0
Author: Arty, 2020-12-17 17:21:25