I/O

  • There's Read, BufRead and Write traits

    • These provide read() and write() methods very similar to UNIX, with the dumb interface

      let mut bytes = [0u8;1024];
      let nread = f.read(&mut bytes).unwrap();
      let nwritten = g.write(&bytes).unwrap();
      
  • There's also BufReader and BufWriter structs

    • These can be used to wrap a Read or Write unbuffered type to get a buffered type.

Readers and Writers

  • Any sensible thing, including Strings and Vecs, is Read / Write

  • The Cursor struct is used for simulating a seekable thing

  • Can implement the Read and Write traits for your own structs

Rust Stdio

  • stdin, stdout and stderr are treated as functions returning readers / writers, because locking across threads

  • Note that the types of stdin, stdout and stderr are different

  • Lock stdin to get a BufRead struct. stdout is already auto-buffered. stderr should rarely be buffered

  • The flush situation is non-optimal

  • Stdio performance is absolute garbage, so don't use it if you're in any kind of hurry (http://github.com/BartMassey/fasthello).

  • Can instead "roll-your-own" on some per-OS basis (http://github.com/BartMassey/nonstdio)

  • examples/stdio.rs

File I/O and Paths

  • Path / PathBuf deal with filenames / pathnames

    • PathBuf is just a wrapper on OsString, which means it can contain any byte sequence: be careful
  • OsString / OsStr sometimes to deal with file contents

Network I/O

  • General networking out of scope for this course

  • TCP provides a stream abstraction that works well with Rust I/O

An Example

http://github.com/pdx-cs-rust/net-15

Last modified: Tuesday, 18 May 2021, 2:02 PM