我刚开始尝试 Rust,所以我实现了 Book 的“猜谜游戏”的修改版本。每次我尝试输入数字时,程序都无法从字符串解析整数:
Guess the number!
Input your guess:
50
You guessed 50
(4 bytes)!
thread 'main' panicked at 'Wrong number format!: ParseIntError { kind: InvalidDigit }', src\libcore\result.rs:997:5
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
error: process didn't exit successfully: `target\release\experiment.exe` (exit code: 101)
Run Code Online (Sandbox Code Playgroud)
下面应用了代码。
我已经尝试过其他整数类型。
use std::io;
use rand::Rng;
fn main() {
println!("Guess the number!\nInput your guess:");
let mut guess = String::new();
let secnum = rand::thread_rng().gen_range(1,101);
'br: loop {
match io::stdin().read_line(&mut guess) {
Ok(okay) => …Run Code Online (Sandbox Code Playgroud)