The subject of this tutorial is file input and output (I/O) in Rust. File I/O is an important part of every operating system. An OS or even a database system wouldn’t be able to function without being able to process, read, write and append to files. Apart from file I/O, in this tutorial we’re going to learn about error handling in Rust and the handy vector data type. In Rust, proper error handling is directly connected to pattern matching as well as the Result and Option data types, which we’ll also touch upon.
File descriptors
A UNIX file descriptor is a positive integer value. UNIX supports three special and standard filenames: /dev/stdin, /dev/stdout and /dev/stderr. These can also be accessed using file descriptors 0, 1 and 2, respectively. In addition, file descriptor 0 can be accessed using /dev/fd/0 – remember that in UNIX and in Linux everything is a file. Later in this tutorial, we’re going to discuss the use of stdin, stdout and stderr in Rust. Now that we know about file descriptors, let’s crack on with pattern matching in Rust.
Pattern Matching
Pattern matching can be handy, but should be used with caution because it can create bugs and unforeseen side-effects. In Rust we use the match keyword for pattern-matching operations. A match statement should catch all possible. The logic of is found in the following match block: