Linux Format

Code read/write system file tools

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:

You’re reading a preview, subscribe to read more.

More from Linux Format

Linux Format5 min read
Tips For Managing Docker Containers
Everyone knows how containers revolutionised application building and deployment. Using a E disposable stack of containers that make up an app that aren’t using the docker-compose command to manage the stack are missing a trick. It allows the shippin
Linux Format3 min readSecurity
ParrotOS
A security focused operating system that is well suited to the Raspberry Pi 4 2GB and upwards, but not Pi 5 for now. ParrotOS provides a plethora of security auditing tools for both red and blue team members. This is a great distro for learners and n
Linux Format2 min read
Automatic Tracking
TimeSlotTracker has activity monitoring and this includes an optional facility to take screenshots and T place them in a user-defined directory. However, this isn’t the type of intricate user monitoring that some of the other options offer. Idle dete

Related Books & Audiobooks