News for hackers. Stuff that matters.
Re:C++ still isn't cutting it by chrismorgan on Sunday October 18, 2020 @12:45PM (#24819129)
File::open("username.txt")? looks fine to me, given the correct mental model of file systems—it returns an error if opening the file fails. Reading is then a different operation that can yield its own set of errors. Therefore each read is fallible; hence reader.lines().filter_map(Result::ok), which says “read it line by line, and just ignore any errors”. If the file is deleted before you’re done reading it, further reads may fail (depends on the file system’s behaviour), and be properly handled as specified.

If you want atomic open-and-read-the-whole-file, you can reach for something like std::fs::read_to_string (https://doc.rust-lang.org/stable/std/fs/fn.read_to_string.ht...).

There are no comments on this comment yet.