-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: Explain why/when .lines() returns an error #37744
Comments
As a general rule for IO: it's interacting with an environment that's outside of the program's control, so errors can never really be ruled out. When BufReader’s operations return an error depends on which A good reason to not use Below there's a new example. The both read all functions do the same thing. Using collect() felt a bit cryptic for an example, but that's what you would use for that particular task (just gathering the results from an iterator). In general, lots of functions will be using use std::io;
use std::io::prelude::*;
fn main() {
let stdin = io::stdin();
let lines = match read_all_lines(stdin.lock()) {
Err(err) => {
println!("Failed to read input: {}", err);
::std::process::exit(1);
}
Ok(result) => result,
};
// continue the program with lines: Vec<String>
}
fn read_all_lines<R: BufRead>(reader: R) -> Result<Vec<String>, io::Error> {
// magic: .collect() can transform an iterator of Result<T, E> into a Result<Vec<T>, E>!
reader.lines().collect()
}
fn read_all_lines_2<R: BufRead>(reader: R) -> Result<Vec<String>, io::Error> {
let mut lines = Vec::new();
for line_result in reader.lines() {
// magic? return with an error if there is an error,
// otherwise push the String to the vector.
lines.push(line_result?);
}
Ok(lines)
} |
Yeah, that seems a bit overcomplicated for the example in that section, and I like that the current example keeps the lines as an iterator instead of collecting. What I'm looking for is a note like "Note that stdin.lines() may return an ErrorKind::Interrupted when ^C is hit, which would panic on the unwrap()" or something. What got me here was trying to figure out what errors could happen when reading from a std::fs::File wrapped in a BufReader, or whether the unwrap() I had seen in every single example was actually safe. |
Good request. The docs should indicate what kind of reading is done and if it would return the Interrupted error. |
Docs: Explain why/when `.lines()` returns an error Fix rust-lang#37744.
context: I'm doing the newbie Rust thing of writing some program that reads lines of a file. From stackoverflow I find myself at
BufReader.lines()
and thenBufRead.lines()
docs.What I'm finding confusing in the docs is that
lines()
returns a Result, but the example in the docs blindlyunwrap()
s that result, and so does every caller oflines()
I find in demo code. It would be nice if BufRead (or, if it's more appropriate there, then maybe in BufReader's impl) would explain under what circumstances you'd get an error instead ofOk
when reading a file, so I know ifunwrap()
is appropriate to use or not.The text was updated successfully, but these errors were encountered: