Book Image

Rust Essentials - Second Edition

By : Ivo Balbaert
Book Image

Rust Essentials - Second Edition

By: Ivo Balbaert

Overview of this book

Rust is the new, open source, fast, and safe systems programming language for the 21st century, developed at Mozilla Research, and with a steadily growing community. It was created to solve the dilemma between high-level, slow code with minimal control over the system, and low-level, fast code with maximum system control. It is no longer necessary to learn C/C++ to develop resource intensive and low-level systems applications. This book will give you a head start to solve systems programming and application tasks with Rust. We start off with an argumentation of Rust's unique place in today's landscape of programming languages. You'll install Rust and learn how to work with its package manager Cargo. The various concepts are introduced step by step: variables, types, functions, and control structures to lay the groundwork. Then we explore more structured data such as strings, arrays, and enums, and you’ll see how pattern matching works. Throughout all this, we stress the unique ways of reasoning that the Rust compiler uses to produce safe code. Next we look at Rust's specific way of error handling, and the overall importance of traits in Rust code. The pillar of memory safety is treated in depth as we explore the various pointer kinds. Next, you’ll see how macros can simplify code generation, and how to compose bigger projects with modules and crates. Finally, you’ll discover how we can write safe concurrent code in Rust and interface with C programs, get a view of the Rust ecosystem, and explore the use of the standard library.
Table of Contents (13 chapters)

Our first program

Let's get started by showing a welcome message to the players of our game. Open your favorite text editor (like Notepad++ or gedit) for a new file and type in the following code:

// code in Chapter1\code\welcome.rs 
fn main() { 
    println!("Welcome to the Game!"); 
} 

The steps to be performed are as follows:

  1. Save the file as welcome.rs. The .rs extension is the standard extension of Rust code files. Source file names may not contain spaces; if they contain more than one word, you can use an underscore, _, as a separator, for example: start_game.rs.
  2. Then compile it to native code on the command line with rustc.welcome.rs. This produces an executable program, welcome.exe, on Windows or welcome on Linux.
  3. Run this program with welcome or./welcome to get the output:
Welcome to the Game!  

The output executable gets its name from the source file. If you want to give the executable another name, like start, compile it with the option -o output_name, as shown below:

rustc welcome.rs -o start

The rustc -O produces native code optimized for execution speed (equivalent to rustc -C opt-level=2); the most optimized code is generated for rustc -C opt-level=3.

Compiling and running are separate consecutive steps, contrary to dynamic languages like Ruby or Python where these are performed in one step.

Let's explain the code a bit. If you have already worked in a C, or Java, or C# like environment, this code will seem quite familiar. As in most languages, execution of code starts in a main() function, which is mandatory in an executable program.

In a larger project with many source files, the file containing the main() function would be called main.rs by convention.

We see that main() is a function declaration because it is preceded by the keyword fn, short and elegant like most Rust keywords. The () after main denotes the parameter list, which is empty here. The function's code is placed in a code block, surrounded by curly braces { }, where the opening brace is put by convention on the same line as the function declaration, but separated by one space. The closing brace appears after the code, in the column right beneath fn.

Our program has only one line, which is indented by four spaces to improve readability (Rust is not whitespace sensitive). This line prints the string Welcome to the Game!. Rust recognizes this as a string, because it is surrounded by double quotes " ". This string was given as argument to the println! macro (the ! indicates it is a macro and not a function). The code line ends in a semicolon, ;, as most, but not all, code lines in Rust do (see Chapter 2, Using Variables and Types).

Exercises:
Write, compile, and execute a Rust program, name.rs, that prints out your name.
What is the smallest possible program in Rust in terms of code size?

The println! macro has some nice formatting capabilities and at the same time checks when compiling whether the type of variables is correct for the applied formatting (see Chapter 2, Using Variables and Types).

Working with Cargo

Cargo is Rust's package and dependency manager, like Bundler, npm, pub, or pip for other languages. Although you can write Rust programs without it, Cargo is nearly indispensable for any larger project. It works the same whether you work on a Windows, Linux, or OS X system. The installation procedure from the previous section includes the Cargo tool, cargo, so Rust is shipped with the batteries included.

Cargo does the following things for you:

  • It makes a tidy folder structure and some templates for your project, with the following command:
    cargo new  
  • It compiles (builds) your code, using the following command:
    cargo build
  • It runs your project, using the following command:
    cargo run
  • If your project contains unit-tests, it can execute them for you, using the following command:
    cargo test
  • If your project depends on packages, it will download them and it will build these packages according to the needs of your code, using the following command:
    cargo update

We'll introduce how to use Cargo now, and we'll come back to it later, but you can find more info at http://doc.crates.io/guide.html.

Let's remake our first project, welcome, using Cargo through the following steps:

  1. Start a new project, welcomec, with the following command:
cargo new welcomec --bin  
  • The option --bin tells Cargo that we want to make an executable program (a binary). This outputs the message Created binary (application) `welcomec` project and creates the following directory structure:
  • A folder with the same name as the project is created as a local Git project. In this folder, you can put all kinds of general info such as a License file, a README file, and so on. Also, a subfolder, src, is created, containing a template source file named main.rs (this contains the same code as our welcome.rs, but prints out the string "Hello, world!").
  • The file Cargo.toml (with a capital C) is the configuration file or manifest of your project; it contains all the metadata Cargo needs to compile your project. It follows the so called TOML format (for more details about this format, see https://github.com/toml-lang/toml), and contains the following text with information about the project:
[package] 
name = "welcomec" 
version = "0.1.0" 
authors = ["Your name <[email protected]>"] 
[dependencies]
  • This file is editable and other sections can be added. For example, you can add a section to tell Cargo that we want a binary with name:
welcome: 
[[bin]]   
name = "welcome" 
  1. We build our project (no matter how many source files it contains) with the following command:
cargo build
  • Which gives us the following output (on Linux):
    Compiling welcomec v0.1.0 (file:///home/ivo/Rust_Book/welcomec)
    Finished dev [unoptimized + debuginfo] target(s) in 0.66 secs
  • Now, the following folder structure is produced:
  • The target/debugdirectory contains the executable welcome.
  1. To execute this program, give the following command:
    cargo run
  • Which produces as output:
    Running `target/debug/welcome`
    Hello, world!

Step 2 has also produced a file called Cargo.lock; this is used by Cargo to keep track of dependencies in your application. At this moment, it contains only the following:

[root] 
name = "welcomec" 
version = "0.1.0" 

The same format is used to lock down the versions of libraries or packages your project depends on. If your project is built in the future, when updated versions of the libraries are available, Cargo will make sure that only the versions recorded in Cargo.lock are used, so that your project is not built with an incompatible version of a library. This ensures a repeatable build process.

The cargo -list gives you an overview of the commands you can use within this tool.

Exercise:
Make, build, and run a project, name, that prints out your name with Cargo.

The site https://crates.io/ is the central repository for Rust packages, or crates as they are called, containing over 10000 crates at the end of June 2017. You can search for crates with specific terms, or browse them alphabetically or by number of downloads. The site looks like the following: