“不匹配的类型:预期的 `i16`,发现 `Result`”在猜数字游戏中
发布时间:2022-06-14 07:42:42 250
相关标签:
我正在用Rust编写一个随机数字猜测程序,但当我检查实际数字时,我得到一个错误,上面写着;预期i16
,找到枚举std::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::().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);
}
}
}
我不理解这个问题,我想只是检查一下它是否会发生,然后继续。
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报