Rust Essentials - Second Edition

4.4 (5 reviews total)
By Ivo Balbaert
    What do you get with a Packt Subscription?

  • Instant access to this title and 7,500+ eBooks & Videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Free Chapter
    Starting with Rust
About 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.
Publication date:
November 2017


Chapter 1. Starting with Rust

Rust is a programming language developed at Mozilla Research and backed up by a big open source community. Its development was started in 2006 by language designer Graydon Hoare. Mozilla began sponsoring it in 2009 and it was first presented officially in 2010. Work on this went through a lot of iterations, culminating in early 2015 in the first stable production, version 1.0.0, developed by the Rust Project Developers, consisting of the Rust team at Mozilla and an open source community of over 1800 contributors. Since then, Rust has developed in a steady pace; its current stable version is 1.20.0.

Rust is based on clear and solid principles. It is a systems programming language, equaling C and C++ in its capabilities. It rivals idiomatic C++ in speed, but it lets you work in a much safer way by forbidding code that could cause program crashes due to memory problems. Moreover, it makes concurrent programming and parallel execution on multi-core machines memory safe without garbage collection--it is the only language that does that. By design, Rust eliminates the corruption of shared data through concurrent access, called data races.

This chapter will present you with the main reasons why Rust's popularity and adoption are steadily increasing. Then, we'll set up a working Rust development environment.

We will cover the following:

  • The advantages of Rust
  • The trifecta of Rust--safe, fast and concurrent
  • The stability of Rust and its evolution
  • The success of Rust
  • Using Rust
  • Installing Rust
  • The Rust compiler
  • Our first program
  • Working with Cargo
  • Developer tools
  • The Standard Library

The advantages of Rust

Mozilla is a company known for its mission to develop tools for and drive the open standards web, most notably through its flagship browser Firefox. Every browser today, including Firefox, is written in C++, some 1,29,00,992 lines of code for Firefox, and 44,90,488 lines of code for Chrome. This makes them fast, but it is inherently unsafe because the memory manipulations allowed by C and C++ are not checked for validity. If the code is written without the utmost programming discipline on the part of the developers, program crashes, memory leaks, segmentation faults, buffer overflows, and null pointers can occur at program execution. Some of these can result in serious security vulnerabilities, all too familiar in existing browsers. Rust is designed from the ground up to avoid those kind of problems.

Compared to C or C++, on the other side of the programming language spectrum we have Haskell, which is widely known to be a very safe and reliable language, but with very little or no control at the level of memory allocation and other hardware resources. We can plot different languages along this control that is safety axis, and it seems that when a language is safer, like Java compared to C++, it loses low-level control. The inverse is also true; a language that gives more control over resources like C++ provides much less safety.

Rust is made to overcome this dilemma by providing:

  • High safety through its strong type system and smart compiler
  • Deep but safe control over low-level resources (as much as C or C++), so it runs close to the hardware

Its main website,, contains links to installation instructions, docs and the Rust community.

Rust lets you specify exactly how your values are laid out in memory and how that memory is managed; that's why it works well at both ends of the control and safety line. This is the unique selling point of Rust, it breaks the safety-control dichotomy that, before Rust, existed among programming languages. With Rust they can be achieved together without losing performance.

Rust can accomplish both these goals without a garbage collector, in contrast to most modern languages like Java, C#, Python, Ruby, Go, and the like. In fact Rust doesn't even have a garbage collector yet (though an optional garbage collector is being designed).

Rust is a compiled language: the strict safety rules are enforced by the compiler, so they do not cause runtime overhead. As a consequence, Rust can work with a very small runtime, so it can be used for real-time or embedded projects and it can easily integrate with other languages or projects.

Rust is meant for developers and projects where performance and low-level optimizations are important, but also where a safe and stable execution environment is needed. The robustness of the language is specifically suited for projects where that is important, leading to less pressure in the maintenance cycle. Moreover, Rust adds a lot of high-level functional programming techniques to the language, so that it feels at the same time like a low-level and a high-level language.

The trifecta of Rust - safe, fast, and concurrent

Rust is not a revolutionary language with new cutting-edge features, but it incorporates a lot of proven techniques from older languages, while massively improving upon the design of C++ in matters of safe programming.

The Rust developers designed Rust to be a general purpose and multi-paradigm language; like C++, it is an imperative, structured and object-oriented language. Besides that, it inherits a lot from functional languages on the one hand, while also incorporating advanced techniques for concurrent programming on the other hand.

The typing of variables is static (because Rust is compiled) and strong. However, unlike in Java or C++, the developer is not forced to indicate types for everything; the Rust compiler is able to infer types in many cases.

C and C++ applications are known to be haunted by problems that often lead to program crashes or memory leaks, and which are notoriously difficult to debug and solve. Think about dangling pointers, buffer overflows, null pointers, segmentation faults, data races, and so on. The Rust compiler (called rustc) is very intelligent and can detect all these problems while compiling your code, thereby guaranteeing memory safety during execution. This is done by the compiler, retaining complete control over memory layout, but without needing the runtime burden of garbage collection (see Chapter 6, Using Traits and OOP in Rust). Of course, safety also implies much less possibility for security breaches.

Rust compiles to native code like Go and Julia but, in contrast to the other two, Rust needs no runtime with garbage collection. In this respect, it also differs from Java and the languages that run on the JVM, like Scala and Clojure. Most other popular modern languages, like .NET with C# and F#, JavaScript, Python, Ruby, Dart, and so on, all need a virtual machine and garbage collection for their execution.

Rust provides several mechanisms for concurrency and parallelism. The Standard Library gives a model that works with threads to perform work in parallel, where each thread maps to an operating system thread. They do not share heap memory, but communicate data through channels and data races are eliminated by the type system (see Chapter 8, Organizing Code and Macros). If needed in your project, several crates provide an actor-model approach with lightweight threads. These mechanisms make it easy for programmers to leverage the power of the many CPU cores available on current and future computing platforms.

The rustc compiler is completely self-hosted, which means it is written in Rust and can compile itself by using a previous version. It uses the LLVM compiler framework as its backend (for more info, see, and produces natively executable code that runs blazingly fast, because it compiles to the same low-level code as C++ ( see some benchmarks at

Rust is designed to be as portable as C++ and to run on widely-used hardware and software platforms. At present, it runs on Linux, macOS X, Windows, FreeBSD, Android, and iOS. For a more complete overview of where Rust can run, see

Rust can call C code as simply and efficiently as calling C code from C itself, and, conversely C code can also call Rust code (see Chapter 9, Concurrency - Coding for Multicore Execution).

Rust developers are called rustaceans.

Other Rust characteristics that will be discussed, in more detail in the later chapters are as follows:

  • Variables are immutable by default (see Chapter 2,Using Variables and Types)
  • Enums (see Chapter 4,Structuring Data and Matching Patterns)
  • Pattern matching (see also Chapter 4,Structuring Data and Matching Patterns)
  • Generics (see Chapter 5,Higher Order Functions and Error-Handling)
  • Higher-order functions and closures (see also Chapter 5,Higher Order Functions and Error-Handling)
  • An interface system called traits (see Chapter 6,Using Traits and OOP in Rust)
  • A hygienic macro system (see Chapter 8,Organizing Code and Macros)
  • Zero-cost abstractions, which means that Rust has higher-language constructs, but these do not have an impact on performance

In conclusion, Rust gives you ultimate power over memory allocation, as well as removing many security and stability problems commonly associated with native languages.

Comparison with other languages

Dynamic languages such as Ruby or Python give you the initial speed of coding development, but the price is paid later in:

  • Writing more tests
  • Runtime crashes
  • Production outages

The Rust compiler forces you to get a lot of things right from the beginning at compile time, which is the least expensive place to identify and fix bugs.

Rust's object orientation is not as explicit or evolved as common object-oriented languages such as Java, C# or Python, as it doesn't have classes. Compared with Go, Rust gives you more control over memory and resources and so it lets you code on a lower level. Go also works with a garbage collector; it has no generics and no mechanism to prevent data races between its goroutines used in concurrency. Julia is focused on numerical computing performance, works with a JIT compiler, and also doesn't give you that low-level control as Rust does.


The stability of Rust and its evolution

Rust started out with version 1.0.0, and, at the time of writing, the current version is 1.20.0. Version numbers follow the semantic versioning principle (see for further information):

  • Patch release: For bug fixes and other minor changes, increment the last number, for example 1.18.1
  • Minor release: For new features which don't break existing features, increment the middle number, for example 1.19.0
  • Major release: For changes which break backwards compatibility, increment the first number, for example 2.0.0

So, no breaking changes will occur during the current 1.n.m cycle versions, as this cycle is backward compatible; Rust projects which are developed in the older versions of this cycle will still compile in a more recent version. However, to be able to work with new features which are only contained in the more recent version, it is mandatory to compile your code to that specific version.

Rust has a very dynamic cycle of progression. Work is performed on three releases (called channels or builds simultaneously)--nightly, beta, and stable, and they follow a strict six-week release cycle like web browsers:

  • The stable channel is the current stable release, which is advocated for Rust projects that are being used in production.
  • The beta channel is where new features are deemed stable enough to be tested out in bigger, non-deployed projects.
  • The nightly channel build contains the latest experimental features; it is produced from the master branch every 24 hours. You would use it only for experimentation.

The beta and stable channel builds are only updated as new features are backported to their branch. With this arrangement, Rust allows users to access new features and bug fixes quickly.

Here is a concrete example: 1.18 was released on 18th June, 2017, the 1.19-beta was released at the same time, and the master development branch was advanced to 1.20. Six weeks later, on 30th July, Rust 1.19 will come out of beta and become a stable release, 1.20 will be promoted to 1.21-beta, and the master will become the eventual 1.21.

Some features in an experimental stage can only work when the code contains an attribute #[feature]. These may not be used on the stable release channel, only on a beta or nightly release; an example is the box syntax (see chapter 2\code\


The success of Rust

Since its production release 1.0, Rust has enjoyed quite a steady uptake. This is manifest if you view a Google Trends survey:

In the well-known TIOBE Index (see, it reached 50th place in September 2015 and is now ranked in 37th position.

In the RedMonk ranking of programming languages (see, it is ready to join the popularity of Lua, CoffeeScript, and Go.

Also, for two consecutive years, Rust was the most loved programing language on Stack Overflow (see

As a hallmark of its success, today, more than 50 companies are using Rust in production, see, amongst which are HoneyPot, Tilde, Chef, npm, Canonical, Coursera, and Dropbox.

Where to use Rust

It is clear from the previous sections that Rust can be used in projects that would normally use C or C++. Indeed, many regard Rust as a successor to, or a replacement for, C/C++. Although Rust is designed to be a systems language, due to its richness of constructs, it has a broad range of possible applications, making it an ideal candidate for applications that fall into one or all of the following categories:

  • Client applications, like browsers
  • Low-latency, high-performance systems, like device drivers, games and signal processing
  • Highly distributed and concurrent systems, like server applications and microservices
  • Real-time and critical systems, like operating systems or kernels
  • Embedded systems (requiring a very minimal runtime footprint) or resource-constrained environments, like Raspberry Pi and Arduino, or robotics
  • Tools or services that can't support the long warmup delays common in just-in-time (JIT) compiler systems and need instantaneous startup
  • Web frameworks
  • Large-scale, high-performance, resource intensive, and complex software systems

Rust is especially suitable when code quality is important, that is, for the following:

  • Modestly-sized or larger development teams
  • Code for long-running production use
  • Code with a longer lifetime that requires regular maintenance and refactoring
  • Code for which you would normally write a lot of unit tests to safeguard


Mozilla uses Rust as the language for writing Servo, its new web browser engine designed for parallelism and safety (

Due to Rust's compiler design, many kinds of browser security bugs are prevented automatically. In 2013, Samsung got involved, porting Servo to Android and ARM processors. Servo is itself an open source project with more than 750 contributors. It is under heavy development, and amongst other parts it already has its own CSS3 and HTML5 parser implemented in Rust. It passed the web compatibility browser test ACID2 in March 2014 (

Servo currently supports Linux, OS X, Windows, and Android. Parts of Servo are merged into Gecko (the engine on which Firefox is based), thus lending the Servo project's advancements to Firefox.


Installing Rust

You can install the Rust toolchain on every platform that Rust supports by using the rustup installer tool, which you can find at

On Windows, double-click on the rustup-init.exe file to install the Rust binaries and dependencies. Rust's installation directory (which by default is C:\Users\username\.cargo\bin) is automatically added to the search path for executables. Additionally you may need the C++ build tools for Visual Studio 2013 or later, which you can download from

On Linux and OS X, run the following command in your shell:

curl -sSf | sh

This installs the Rust toolchain in /home/username/.cargo/bin by default.

Verify the correctness of the installation by showing Rust's version by typing rustc -V or rustc - -version in a console, which produces output like the following:

Rust can be uninstalled by running the following command:

rustup self uninstall

The rustup tool enables you to easily switch between stable, beta, and nightly compilers and keep them updated. Moreover, it makes cross-compiling simpler with binary builds of the Standard Library for common platforms.

At is a list of all the platforms on which Rust can run.

A bare metal stack called zinc for running Rust in embedded environments can be found at; at this moment only the ARM architecture is supported.

The source code resides on GitHub (see and, if you want to build Rust from source, we refer you to


rustc--the Rust compiler

The Rust installation directory containing rustc can be found on your machine in the following folder (unless you have chosen a different installation folder):

  • On Windows at C:\Users\username\.cargo\bin
  • On Linux or OS X in /home/username/.cargo/bin

rustc and the other binaries can be run from any command-line window. The rustc command has the following format:

rustc [options] input

The options are one-letter directives for the compiler after a dash, like -g or -W, or words prefixed by a double dash, like - -test or - -version. All options with some explanation are shown when invoking rustc -h. In the next section, we verify our installation by compiling and running our first Rust program.

To view a local copy of the Rust documentation as a website, type rustup doc into a terminal.


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\ 
fn main() { 
    println!("Welcome to the Game!"); 

The steps to be performed are as follows:

  1. Save the file as 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:
  2. Then compile it to native code on the command line with 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 -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 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,, 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

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` projectand 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 (this contains the same code as our, 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, and contains the following text with information about the project:
name = "welcomec" 
version = "0.1.0" 
authors = ["Your name <>"] 
  • 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:
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:

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 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:


Developer tools

Because Rust is a systems programming language, the only thing you need is a good text editor (but not a word proecessor) for writing the source code, and everything else can be done using commands in a terminal session. However, some developers appreciate the functionalities offered by more fully-fledged text editors specific for programming or Integrated Development Environments (IDEs). Rust has a lot of possibilities in this regard. Most Rust developers work with Vim or Emacs, but Rust plugins exist for a host of text editors, like Atom, Brackets, BBEdit, Emacs, Geany, gedit, Kate, TextMate, Textadept, Vim, NEdit, Notepad++, Sublime Text, and Visual Studio Code. Also, some IDEs, such as Eclipse (RustDT), Netbeans (rust-netbeans), IntelliJ, and Visual Studio, provide plugins for Rust; see the updated overview at

These come with a varying range of features, such as syntax highlighting, code formatting, code completion, linting, debugging, Cargo project support, and so on.

Using Sublime Text

The plugins for the popular Sublime Text editor ( are particularly pleasant to work with and don't get in your way. After having installed Sublime Text (you might want to get a registered version if you start using it regularly), also install the Package Control package (for instructions on how to do that visit

Then, to install the Sublime Text Rust plugin, open the command palette in Sublime Text (Ctrl+Shift+Pon Windows or cmd+Shift+P on OS X) and select Package Control |Install Package and then select RustEnhanced from the list.

Sublime Text is a very complete text editor, including color schemes. The Rust plugin provides syntax highlighting and auto-completion; type one or more letters, choose an option from the list that appears with an arrow key and then press Tab to insert the code snippet, or simply select a list option through a mouse click.

When working with Rust code, select Tools | Build System and RustEnhanced.

Then, you can run and compile a source file with Ctrl+B. Warnings or errors appear in the lower panel; if everything is OK the output of the program appears together with a message like the following:

[Finished in 0.6s]

If you want to do the two steps separately, do Ctrl+Shift+B. A pop-up menu appears, click on RustEnhanced if you only want to compile, click on RustEnhanced |Run if you want to execute the program. A SublimeLinter plugin exists that provides an interface to rustc, called SublimeLinter-contrib-rustc. It does additional checks on your code for stylistic or programming errors. You can install it as indicated above through Package Control and then use it from the menu Tools |SublimeLinter (for more details consult

There are also plugins for IDEs like:

You can test out Rust code even without local installation with the Rust Playground at, which allows you to edit or paste your code and evaluate it.

The interactive shell rusti or Read-Evaluate-Print-Loop (REPL) is in development for Rust, which is common for dynamic languages, but remarkable for a statically compiled language. You can find it at

An online environment that combines both is Refer the following link for more details


The Standard Library

Rust's Standard Library, stdlib, contains all primitive types, basic modules, and macros. In fact, nearly all of this book talks about it, with Chapter 11, Exploring the Standard Library filling in some gaps.

It is the well-tested and minimal code that ensures portability to a wide diversity of platforms and on which is built the rest of the ecosystem.

Having installed Rust also means that you have a binary version of the Standard Library on your system. When you compile source code or do a cargo build, this stdlib is included; this explains why the executable file size is not that small (for example 129 KB for welcomec.exe on Windows).

Compile with rustc -C prefer-dynamic to get a small executable, like 10 KB for welcome.



In this chapter, we got an overview of Rust's characteristics, where Rust can be applied and compared it to other languages. We made our first program, demonstrated how to build a project with Cargo, and gave you choices on how to make a more complete development environment.

In the following chapter, we will look at variables and types, and explore the important concept of mutability.

About the Author
  • Ivo Balbaert

    Ivo Balbaert is a lecturer in web programming and databases at CVO Antwerpen, a community college in Belgium. He received a PhD in applied physics from the University of Antwerp in 1986. He worked for 20 years in the software industry as a developer and consultant in several companies, and for 10 years as project manager at the University Hospital of Antwerp. From 2000 onwards, he switched to partly teaching and partly developing software (at KHM Mechelen, CVO Antwerpen). He also wrote an introductory book in Dutch about developing in Ruby and Rails, Programmeren met Ruby en Rails, published by Van Duuren Media. In 2012, he authored a book on the Go programming language, The Way to Go, published by iUniverse. He has written a number of introductory books for new programming languages, notably Dart, Julia, Rust, and Red, all published by Packt.

    Browse publications by this author
Latest Reviews (5 reviews total)
Good quality information at a good price.
As part of my New Year resolution to learn Rust language
Good books for a good price.
Recommended For You
Rust Essentials - Second Edition
Unlock this book and the full library FREE for 7 days
Start now