I/O
I/O
There's
Read
,BufRead
andWrite
traitsThese provide
read()
andwrite()
methods very similar to UNIX, with the dumb interfacelet mut bytes = [0u8;1024]; let nread = f.read(&mut bytes).unwrap(); let nwritten = g.write(&bytes).unwrap();
There's also
BufReader
andBufWriter
structs- These can be used to wrap a
Read
orWrite
unbuffered type to get a buffered type.
- These can be used to wrap a
Readers and Writers
Any sensible thing, including
String
s andVec
s, isRead
/Write
The
Cursor
struct is used for simulating a seekable thingCan implement the
Read
andWrite
traits for your own structs
Rust Stdio
stdin
,stdout
andstderr
are treated as functions returning readers / writers, because locking across threadsNote that the types of
stdin
,stdout
andstderr
are differentLock
stdin
to get aBufRead
struct.stdout
is already auto-buffered.stderr
should rarely be bufferedThe
flush
situation is non-optimalStdio 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 / pathnamesPathBuf
is just a wrapper onOsString
, 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