Can't parse String from stdin to floating-poin

2020-04-19 07:06发布

问题:

When parsing a String type value to a floating-point type value in Rust, everything works fine with
"let pi: f64 = 3.14".parse().unwrap();.

However, when parsing a String type value that comes from standard input, even if it's the exact same value, the program panics and throws:

thread 'main' panicked at 'called Result::unwrap() on an Err value: ParseFloatError { kind: Invalid }', src/libcore/result.rs:999:5 note: run with RUST_BACKTRACE=1 environment variable to display a backtrace.

I checked the value's type, and it is a String, so I don't understand what the error is about, and I haven't been able to find anything related specifically to standard input (stdin) and this issue. Has anyone else come across this? Is there a good way to prevent the panic?

Here is some code to replicate the issue:

use std::io::{stdin,stdout,Write};

fn main() {
    let mut s = String::new();

    println!("Give a number ");

    stdin().read_line(&mut s)
        .expect("Did not enter a correct string");

    let user_input: f64 = s.parse().unwrap();

    println!("{:?}", user_input)
}

Thanks in advance!

回答1:

As suggested by SCappella the new line needs to be removed. The following will work:

let user_input: f64 = s.trim().parse().unwrap();


回答2:

Much thanks to Jeff Muizelaar and SCappella!

SCappella pointed out that there is a newline character causing the type conversion to fail, and Jeff Muizelaar pointed out that adding a trim() method to the String, "s," would remove the newline, resolving the issue.

After realizing that was the issue, I was able to do some more accurate Googling and found this bit of Rust documentation. It explains that the read_line() method

"read[s] all bytes until a newline (the 0xA byte) is reached...Once found, all bytes up to, and including, the delimiter (if found) will be appended..."