The final part of our tour of getting information in and out of a program is using files. As far as Rust is concerned, a file is just another stream, with the exception that this stream goes elsewhere.
It is important when using anything to do with files that the try!
macro is used to trap all errors.
Here, we are going to use std::io
, std::io::prelude::*
and std::fs::File
. std::io
is the standard input/output library, the *
after prelude means to use anything in the prelude library, and std::fs
is the filesystem library.
Note
Filesystem calls are very platform-specific; Windows users use the likes of C://Users/Paul/Documents/My Documents
for the user's home directory, whereas Linux and macOS machines would use ~/
for the user's home directory. If a path is not given for a file, the program will assume the file is in the same directory in which the binary resides.