Can't parse String from stdin to floating-point - Rust

前端 未结 2 1834
孤城傲影
孤城傲影 2021-01-21 13:36

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,

2条回答
  •  情书的邮戳
    2021-01-21 14:07

    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..."

提交回复
热议问题