rust - 如何解决 rust 中的 “value: ParseIntError”?

标签 rust runtime-error rust-cargo panic

我正在尝试在Rust中构建华氏度到摄氏度的转换器。

我编译成功,但是我不知道运行时出了什么问题。这是因为转换了吗?

这是我的代码:

use std::io;

fn main(){
    println!("Please select\n 1.CELSIUS to FAHRENHEIT\n 2.FAHRENHEIT to CELSIUS");

    const CEL_FAH: f64 = 32.00;
    const FAH_CEL: f64 = -17.22;

    let mut select = String::new();

    io::stdin().read_line(&mut select)
    .expect("Please select the appropriate options");

    let select: i32 = select.parse().unwrap();

    //control flow

    if select == 1 {
        println!("1. CELSIUS - FAHRENHEIT: ");

        let cels = String::new();

        let cels: f64 = cels.parse().unwrap();

        let ans1 = cels * CEL_FAH;

        println!("Conversions: {}", ans1);
    } else if select == 2 {

        println!("2. FAHRENHEIT - CELSIUS: ");

        let fahs = String::new();

        let fahs: f64 = fahs.parse().unwrap();

        let ans2 = fahs * FAH_CEL;

        println! ("Conversions: {}", ans2);
    }else {

        println!("Select the options please.");
    }


}

这是我的输出和错误:
   Compiling converter v0.1.0 (D:\Program Files\Rust Projects\converter)
    Finished dev [unoptimized + debuginfo] target(s) in 2.46s
     Running `target\debug\converter.exe`
Please select
 1.CELSIUS to FAHRENHEIT
 2.FAHRENHEIT to CELSIUS
2
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ParseIntError { kind: InvalidDigit }', src\main.rs:19:23
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\converter.exe` (exit code: 101)```

最佳答案

在您的代码中,存在三个错误:

  • 当您使用stdin()方法获取输入并想将其解析为另一种类型时,必须放入.trim(),因为stdin()在输入中添加了空格,因此您需要修剪那些不需要的空格。
  • 每次输入时,都必须使用io::stdin()方法和read_line方法,以便编译器保留用户输入的内容。
  • 您使用了错误的公式进行转换。

  • 我已经在您的代码中进行了更正,现在可以正常工作了,以下是代码段:
    use std::io;
    
    fn main() {
        println!("Please select\n 1.CELSIUS to FAHRENHEIT\n 2.FAHRENHEIT to CELSIUS");
    
        const CEL_FAH: f64 = 32.00;
        const FAH_CEL: f64 = 1.8; //changed
    
        let mut select = String::new();
    
        io::stdin()
            .read_line(&mut select)
            .expect("Please select the appropriate options");
    
        let select: i32 = select.trim().parse().unwrap(); // .trim() method requires when taking input
    
        //control flow
    
        if select == 1 {
            println!("1. CELSIUS - FAHRENHEIT: ");
    
            let mut cels = String::new();
            io::stdin()
                .read_line(&mut cels)
                .expect("Please input a temperature in degrees"); // when you're taking input from user you have to use stdin()
    
            let cels: f64 = cels.trim().parse().unwrap();
    
            let ans1 = (cels * FAH_CEL) + CEL_FAH; //this the correct formula to convert from celsius to fahrenheit
    
            println!("Conversions: {}", ans1);
        } else if select == 2 {
            println!("2. FAHRENHEIT - CELSIUS: ");
    
            let mut fahs = String::new();
            io::stdin()
                .read_line(&mut fahs)
                .expect("Please input a temperature in degrees"); //when you're taking input from user you have to use stdin()
    
            let fahs: f64 = fahs.trim().parse().unwrap();
    
            let ans2 = (fahs - CEL_FAH) * 5. / 9.; //this the correct formula to convert from fahrenheit to celsius
    
            println!("Conversions: {}", ans2);
        } else {
            println!("Select the options please.");
        }
    }
    

    您也可以引用此存储库。 Temperature converter rust

    关于rust - 如何解决 rust 中的 “value: ParseIntError”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61168269/

    相关文章:

    java - ViewPager 致命异常 : main

    gdb - 如何在 GDB 中调试失败的 cargo 测试?

    debugging - 有没有办法在gdb或lldb中直接运行Cargo构建的程序?

    generics - 在 Rust 中使用 traits 的通用函数

    import - 如何 "use"或导入本地 Rust 文件?

    insert - 带 sails-mongo 的 Sails Js 强制类型不正确。我究竟做错了什么?

    ios - CFBundle 运行时错误 - 这是什么意思?

    testing - 如何找到用于 Cargo 测试的资源?

    multithreading - 线程引用需要静态生命周期?

    rust - 为什么在尝试使用外部宏的参数调用内部宏时得到 "unexpected token"?