"mismatched types: expected `i16`, found `Result`"在数字猜谜游戏中



我正在用Rust做一个随机数猜测程序,但是当我检查实际数字时,我得到一个错误,说&;期望i16,发现enumstd::result::Result&;

use rand::Rng; // 0.8.0
use std::io::{stdin, stdout, Write};
use std::process;
use std::result::Result;
fn read(input: &mut String) {
stdout().flush().expect("failed to flush");
stdin().read_line(input).expect("failed to read");
}
fn main() {
loop {
let mut number = String::new();
let rand_ = rand::thread_rng().gen_range(1..10);
let mut killcheck = String::new();
println!("Input a number between 0 and 10 n");
match rand_ {
1..=5 => println!("it is 1 through 5"),
5..=10 => println!("it is 5 through 10"),
_ => continue,
}
read(&mut number);

let number: i16 = number.trim().parse::<i16>().unwrap();
match number {
Ok(ok) => continue,
Err(e) => println!("No number could be found"),
_ => continue
}
read(&mut killcheck);

if killcheck.trim() == "end" {
println!("error is null");
process::exit(0x0100);
}
if number == rand_ {
println!("Currect!n")
}
if number != rand_ {
println!("Incorrect!n")
}
read(&mut killcheck);
if killcheck.trim() == "end" {
println!("error is null");
process::exit(0x0100);
}
}
}

我不明白这个问题,我认为它只是检查它是否可以发生,然后继续。

let number: i16 = number.trim().parse::<i16>().unwrap();  //*number is i16 type with the use of unwrap() instead of Result*
match number {
Ok(ok) => continue,  //*It should be some i16 numbers here instead of Result(Ok/Err)*
Err(e) => println!("No number could be found"),
_ => continue
}

最新更新