繁体   English   中英

在 STDIN 之后编译时获取不匹配的类型,然后对输入进行数学运算

[英]Getting mismatched types when compiling after STDIN and then math on input

我正在尝试编写一个 rust 程序来将输入温度从华氏温度转换为摄氏度,反之亦然。 我对 Rust 还是很陌生,书中提到将这个程序作为一种学习方式。

当我尝试编译代码时,我不断收到错误类型不匹配的错误。 错误可以在下面立即看到,我的代码在错误下方进行了格式化。

error[E0308]: mismatched types
  --> temperature.rs:12:28
   |
12 |     let temperature: u32 = temperature.trim().parse();
   |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected u32, found enum `std::result::Result`
   |
   = note: expected type `u32`
              found type `std::result::Result`

error[E0308]: mismatched types
  --> temperature.rs:34:29
   |
34 |         let fahr_result: f32 = ((temperature * 9) / 5) + 32;
   |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected f32, found u32

error[E0308]: mismatched types
  --> temperature.rs:38:29
   |
38 |         let celc_result: f32 = temperature - 32 * 5 / 9;
   |                                ^^^^^^^^^^^^^^^^^^^^^^^^ expected f32, found u32
use std::io;

fn main () {
    println!("Welcome to the Temperature converter.");
    println!("Enter the temperature value, number only: ");

    let mut temperature = String::new();

    io::stdin().read_line(&mut temperature)
        .expect("Failed to read line");

    let temperature: u32 = temperature.trim().parse();

    println!("Do you want to convert to Celcius or Fahrenheit? (Enter the number of your choice)\n1. Fahrenheit\n2.Celcius");

    let mut temp_choice = String::new();

    io::stdin().read_line(&mut temp_choice)
        .expect("Failed to read line");

    let temp_choice: u32 = temp_choice.trim().parse()
        .expect("Fart in your butt.");

    if temp_choice == 1 {
        let fahr_result: f32 = ((temperature * 9) / 5) + 32;
        println!("{} degrees Celcius is equal to {} degrees Fahrenheit.", temperature, fahr_result);
    } else if temp_choice == 2 {
        let celc_result: f32 = temperature - 32 * 5 / 9;
        println!("{} degrees Fahrenheit is equal to {} degrees Celcius.", temperature, celc_result);
    } else {
        println!("Invalid Choice. Exiting.");
    }
}

Rust 的String.parse返回一个Result<F, <F as FromStr>::Err> ,而不是一个字符串。 最简单的方法是像这样使用Result.unwrap

let temperature: u32 = temperature.trim().parse().unwrap();

请注意,如果parse返回错误结果,则unwrap将 panic,因此您可能需要查看:

  • Result.unwrap_or允许您在失败时提供默认值
  • Result.expect (正如您在其他地方使用的那样),如果成功则返回结果的Ok值,如果Result.expect则使用提供的文本参数恐慌

至于另一个问题,这是由于类型是整数( u32 ),因此数学也是整数(例如,5 / 9 = 0)。 解决这个问题的简单方法是:

let fahr_result: f32 = (((temperature as f32) * 9.) / 5.) + 32.;

和:

let celc_result: f32 = (temperature as f32) - 32. * 5. / 9.;

作为奖励,由于运算顺序,摄氏度结果计算不太正确,因为乘法和除法将在减法之前执行。 您可以通过将temperature - 32括在括号中来解决此问题,如下所示:

let celc_result: f32 = ((temperature as f32) - 32.) * 5. / 9.;

我想我们都在阅读The Rust Programming Language并且我有同样的错误。 我通过以下方式解决了它:

let mut fbradley: f64 = match fbradley.trim().parse() {
    Ok(num) => num,
    Err(_) => 0.0,
};

问题是parse的输出不是浮点数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM