- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下程序,我如何检测标准输入中的 EOF 并打破循环?
use std::io;
use std::process;
fn main() {
let mut sum = 0;
loop {
let mut number_str = String::new();
match io::stdin().read_line(&mut number_str) {
Ok(n) => {},
Err(e) => { println!("ERROR: got '{}' when reading a line", e) }
}
match number_str.trim().parse::<i32>() {
Err(n) => {
println!("ERROR: Entered something that is not a number: '{}'",
number_str.trim_right());
process::exit(1)
},
Ok(n) => { sum += n }
}
}
}
注意:有一个identical question但答案似乎已经过时了,这就是为什么我在问题标题中添加了版本号。
最佳答案
来自documentation for read_line
:
If this reader is currently at EOF then this function will not modify
buf
and will returnOk(n)
wheren
is the number of bytes which were read.
关于rust - 如何在 Rust 1.12 的 read_line 中检查 EOF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41210691/
这个问题在这里已经有了答案: Weird behaviour when using read_line in a loop (1 个回答) 去年关闭。 fn main() { let mut
我的第一个 Rust 程序应该以字符的形式从用户那里获取输入,C 或 F: use std::io; fn main() { let mut srcunit = String::new();
给出下面的代码,我如何专门检查 EOF?或者更确切地说,我如何区分“这里什么都没有”和“它爆炸了”? match io::stdin().read_line() { Ok(l) => prin
在C中,使用之前的scanf或 gets “stdio.h”函数来获取和存储用户输入,程序员必须手动为读取的数据分配内存以存储在其中。在 Rust 中,std::io::Stdin.read_line
考虑这段代码来读取 rust 中的用户输入 use std::io; fn main() { let mut input = String::new(); io::stdin()
我正在尝试从标准输入中获取 String: use std::io; fn ask_nick() -> String { let reader = io::stdin(); let m
当使用 read_line() 读取输入流时,如果您没有在语句之后使用 .expect(),编译器将警告您 这个 `Result` 可能是一个 `Err` 变体,应该被处理。 我想了解的是,什么样的情
下面的代码在调用 read_line(..) 时比 lines() 运行得慢得多你不能在 Playground 上运行它,但对我来说,它会打印以下内容 lines() took Duratio
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 4 年前。 Improve
我是一个绝对的 Rust 初学者,试图构建一个简单的确认功能(是或否),但我无法让用户输入任何内容,该功能只是不断循环而不等待用户输入: "" "" "" etc. 是下面简化版的结果。 use s
考虑以下程序,我如何检测标准输入中的 EOF 并打破循环? use std::io; use std::process; fn main() { let mut sum = 0; lo
我想制作一个函数,从给定的文本文件中读取您选择的一行。继续作为参数的函数(打开的 int fd 和 int line_number)它必须使用 C 语言和 Unix 系统调用(read 和/或 ope
我是一名优秀的程序员,十分优秀!