RUST
Coding secure Rust system tools
Make yourself comfortable, as Mihalis Tsoukalos explains the basics of Rust, so you can write memory-secure system utilities.
Part One
Don’t miss next issue! Subscribe on page 18
OUR EXPERT
Mihalis Tsoukalos is a systems engineer and a technical writer. Find him on Twitter using @mactsouk.
QUICK TIP
Get the for this tutorial from the Linux Format archive: www. linuxformat. com/archives ?issue=288
Rust has become a key language for the Linux kernel and beyond. This series on systems programming with Rust will cover the basics, including memory management, working with command line arguments and environment variables, the cargo tool and writing tests. You’ll learn how to work with files and directories, file I/O, UNIX processes, concurrency and network programming.
Installing Rust
You can use the package manager that comes with your Linux distribution to install Rust. On an Arch Linux machine, you can install Rust by running pacman -S rust with root privileges – this will also install the cargo tool (explained later on). New, stable Rust versions are released every six weeks) and sometimes without full backward compatibility. You can find out what version of Rust you’re using by executing rustc --version . Note: Rust source code files usually have the .rs file extension.
Data types
Let’s look at the basic data types of Rust and how you can define new variables. Because Rust is a statically typed language, it must know the types of all variables of a program at compile time. Additionally, Rust supports both mutable and immutable variables. All Rust variables are immutable unless defined otherwise.
The integer types of Rust have the i format for the signed versions and u format for the unsigned versions. The data type of floating point numbers can be either f32 or f64, depending on the bits used. Rust also supports the Boolean type, which can have the true and false values.
Rust offers support for the character type. A char is always four bytes in size. You define a char variable using single quotes. Rust supports two data types for working with strings: String and str. The String type is for working with mutable strings and has a length and a capacity property. On the other hand, the str type is for working with immutable strings that you want to pass around. You’ll most likely see a str variable be used as &str – put simply, a str variable is accessed as a reference to some UTF-8 data. A str variable is usually called a “string slice” or, even simpler, a “slice”. You can’t add or remove data from an existing str variable.