Hacker News new | ask | show | jobs
by 4bpp 2612 days ago
How does Rust handle these problems in the context of file I/O? I'm sure there is an axiomatically idiomatic (official) implementation of it as part of the language distribution, and at the same time file handles seem conceptually very similar to display handles and should have similar failure patterns (space can run out, a disk can fail, a plug-and-play disk can be yanked out at any time...).
1 comments

That is all here: https://doc.rust-lang.org/stable/std/fs/struct.File.html

These failure patterns are handled by each method, for example,

  pub fn open<P: AsRef<Path>>(path: P) -> Result<File>
that Result will return an error if the file can't be opened, etc. Let's say you want to write some bytes, that's

  fn write(&mut self, buf: &[u8]) -> Result<usize>
This also returns Result, so if you've opened the file, but the disk is now out of space, this will return an error, etc.