簡體   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