Rust 是否有方法检查用户是否输入了带有文本“io'”的整数;让我们读吧!()宏?
我想检查用户是否输入了整数。如果他们没有,我想将他们重定向回输入问题:Rust 是否有方法检查用户是否输入了带有文本“io'”的整数;让我们读吧!()宏?,rust,Rust,我想检查用户是否输入了整数。如果他们没有,我想将他们重定向回输入问题: println!("Place Your Chip"); let mut y_col: usize; loop { y_col = read!(); // Check if user entered in a integer or not if y_col < 1 || y_col > 6 { println!("Column space is 1 to 6");
println!("Place Your Chip");
let mut y_col: usize;
loop {
y_col = read!();
// Check if user entered in a integer or not
if y_col < 1 || y_col > 6 {
println!("Column space is 1 to 6");
continue;
} else {
y_col -= 1;
}
if game.check_column(y_col) {
println!("\t\t\t\t\t\t\t\tThe column you choose is full");
continue;
}
break;
}
println!(“放置芯片”);
让我们彼此交流:使用;
环路{
y_col=读!();
//检查用户是否输入了整数
如果y|col<1 | y|col>6{
println!(“列空间为1到6”);
继续;
}否则{
y_col-=1;
}
如果是游戏。检查列(y列){
println!(“\t\t\t\t\t\t\t\t您选择的列已满”);
继续;
}
打破
}
关键是通过终止线程来处理错误,这样调用者就不必担心这些错误了。这就是存在的原因:
不,如果使用read并输入一个字母,则会在调用Result::unwrap()`on an
Err
value:Parse(“q”,“try\u read\u var”)'时出现thread'main'恐慌`
#[macro_use]
extern crate text_io; // 0.1.7
fn main() {
let mut y_col: Result<usize, _>;
y_col = try_read!();
match y_col {
Ok(v) => println!("Got a number: {}", v),
Err(e) => eprintln!("Was not a number ({})", e),
}
}