我想输入2个用户输入,高度和宽度:
fn main() {
let mut w = String::new();
let mut l = String::new();
println!("Enter the width");
io::stdin().read_line(&mut w).expect("failed to read input");
let w: i32 = w.trim().parse().expect("invalid input");
println!("Enter the length");
io::stdin().read_line(&mut l).expect("failed to read input");
let l: i32 = l.trim().parse().expect("invalid input");
println!("width {:?}", w);
println!("length{:?}", l);
}
有没有更短的方法来实现这一目标?
最佳答案
处理来自终端的输入并不是一件很有趣的事情,因此,许多试图制作的 crate 更短/更容易/更豪华。
可能的候选人是rprompt:
let reply = rprompt::prompt_reply_stdout("Password: ").unwrap();
println!("Your reply is {}", reply);
这可能只是Thomas' advice将所有这些东西包装到您自己的函数中的捷径。我更喜欢使用 crate ,因为它们允许使用一些精美的小功能,例如使用户在输入错误时能够更正其输入内容。prompty可能值得一看。
关于rust - 如何在Rust中从标准中获取多个输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63580989/