I/O

  • Rust I/O is kind of awkward, as befits a C-like language. But it's pretty fast

  • Rust I/O is cross-platform for the Big Three; works fine with all of them

  • Basic support is in std::io, std::fs, std::path, std::process, std::net and a few other places

  • Probably easiest by starting with a good example similar to what you want to do and trying to understand how it works

Basics

  • Basic I/O is on things with the Read, BufRead and Write traits

    • Any sensible thing, including strings, either is Read / Write or can be made to be: see std::io::Cursor for bytes to/from an arbitrary source

    • Read = "unbuffered"; BufRead implements BufReader; there is no BufWrite

  • The BufReader and BufWriter structs can be used to add a buffering wrapper. These are block buffers with a user-specified blocksize

  • playground

Stdio

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

  • The borrow rules will confuse you at first

  • The flush() situation is non-optimal. Standard input is line buffered, standard output is line buffered. This cannot be easily changed. Prompting is quite tricky

  • http://github.com/BartMassey/prompted

Paths

  • std::path is the right way to deal with filenames and pathnames

  • PathBuf is the owned type for Path. String types etc are AsRef<Path>

  • PathBuf has the notion of push() and pop() for pathname components — see the docs

  • PathBuf is an abstraction over OsString, allowing the local path characters and separators to be used correctly

Files

  • Basic open/close etc are fairly obvious

  • Directory operations are supported

  • Usually wrap a file in a BufReader or BufWriter

  • The interface is a bit weird because zero-copy attempts and scatter/gather and stuff

  • std::cursor::Cursor is a neat way to get "fake files" for tests, etc

  • playground

Networking

  • Reasonable TCP/IP interface in std::net

  • The SocketAddr struct deals with ports, which is nice

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

Last modified: Monday, 29 July 2019, 10:26 AM