Contents

Ch.0 — Introduction

The Rust Programming Language

Introduction


Welcome to The Rust Programming Language, an introductory book about Rust. Rust helps you write faster, more reliable software. High-level ergonomics and low-level control are often at odds in programming language design; Rust challenges that conflict. By balancing powerful technical capacity with a great developer experience, Rust lets you control low-level details (such as memory usage) without all the hassle traditionally associated with such control.

Remark. This edition of the book is the same as The Rust Programming Language available in print and ebook format from No Starch Press.

  • Who Rust Is For
  • Who This Book Is For
  • How to Use This Book
  • Source Code

The Rust Programming Language · Introduction

1

Introduction · Who Rust Is For

Teams of developers: the compiler as gatekeeper

Rust is ideal for many people for a variety of reasons. Let’s look at a few of the most important groups.

Teams of Developers

Rust is proving to be a productive tool for collaborating among large teams of developers with varying levels of systems programming knowledge.

Low-level code is prone to various subtle bugs, which in most other languages can only be caught through extensive testing and careful code review by experienced developers.

In Rust, the compiler plays a gatekeeper role by refusing to compile code with these elusive bugs, including concurrency bugs. By working alongside the compiler, the team can spend its time focusing on the program’s logic rather than chasing down bugs.

The Rust Programming Language · Introduction

2

Introduction · Who Rust Is For

Teams of developers: contemporary tools

Rust also brings contemporary developer tools to the systems programming world:

  • Cargo, the included dependency manager and build tool, makes adding, compiling, and managing dependencies painless and consistent across the Rust ecosystem.
  • The rustfmt formatting tool ensures a consistent coding style across developers.
  • The Rust Language Server powers integrated development environment (IDE) integration for code completion and inline error messages.

By using these and other tools in the Rust ecosystem, developers can be productive while writing systems-level code.

The Rust Programming Language · Introduction

3

Introduction · Who Rust Is For

Students and companies

Students

Rust is for students and those who are interested in learning about systems concepts. Using Rust, many people have learned about topics like operating systems development. The community is very welcoming and happy to answer students’ questions. Through efforts such as this book, the Rust teams want to make systems concepts more accessible to more people, especially those new to programming.

Companies

Hundreds of companies, large and small, use Rust in production for a variety of tasks, including command line tools, web services, DevOps tooling, embedded devices, audio and video analysis and transcoding, cryptocurrencies, bioinformatics, search engines, Internet of Things applications, machine learning, and even major parts of the Firefox web browser.

The Rust Programming Language · Introduction

4

Introduction · Who Rust Is For

Open source developers; speed and stability

Open Source Developers

Rust is for people who want to build the Rust programming language, community, developer tools, and libraries. We’d love to have you contribute to the Rust language.

People Who Value Speed and Stability

Rust is for people who crave speed and stability in a language. By speed, we mean both how quickly Rust code can run and the speed at which Rust lets you write programs.

The Rust compiler’s checks ensure stability through feature additions and refactoring. This is in contrast to the brittle legacy code in languages without these checks, which developers are often afraid to modify.

The Rust Programming Language · Introduction

5

Introduction · Who Rust Is For

Zero-cost abstractions and Rust’s ambition

By striving for zero-cost abstractions, Rust endeavors to make safe code be fast code as well.

Definition 0.1 (Zero-cost abstractions). Higher-level features that compile to lower-level code as fast as code written manually.

The Rust language hopes to support many other users as well; those mentioned here are merely some of the biggest stakeholders.

Overall, Rust’s greatest ambition is to eliminate the trade-offs that programmers have accepted for decades by providing safety and productivity, speed and ergonomics.

Give Rust a try, and see if its choices work for you.

The Rust Programming Language · Introduction

6

Introduction · Who This Book Is For

Readers who already program

This book assumes that you’ve written code in another programming language, but it doesn’t make any assumptions about which one.

The material is meant to be broadly accessible to those from a wide variety of programming backgrounds. The book doesn’t spend a lot of time talking about what programming is or how to think about it.

Remark. If you’re entirely new to programming, you would be better served by reading a book that specifically provides an introduction to programming.

The Rust Programming Language · Introduction

7

Introduction · How to Use This Book

Reading order; concept and project chapters

In general, this book assumes that you’re reading it in sequence from front to back. Later chapters build on concepts in earlier chapters, and earlier chapters might not delve into details on a particular topic but will revisit the topic in a later chapter.

You’ll find two kinds of chapters in this book:

Definition 0.2 (Concept chapter). A chapter in which you learn about an aspect of Rust.

Definition 0.3 (Project chapter). A chapter in which we build small programs together, applying what you’ve learned so far.

Chapter 2, Chapter 12, and Chapter 21 are project chapters; the rest are concept chapters.

The Rust Programming Language · Introduction

8

Introduction · How to Use This Book

Chapters 1–3: two ways to start

Chapter 1 explains how to install Rust, how to write a “Hello, world!” program, and how to use Cargo, Rust’s package manager and build tool.

Hands-on: Chapter 2

Chapter 2 is a hands-on introduction to writing a program in Rust, having you build up a number-guessing game. It covers concepts at a high level; later chapters provide additional detail. If you want to get your hands dirty right away, Chapter 2 is the place for that.

Detail first: Chapter 3

A particularly meticulous learner who prefers to learn every detail before moving on might skip Chapter 2 and go straight to Chapter 3, which covers Rust features similar to those of other programming languages; then return to Chapter 2 to work on a project applying those details.

The Rust Programming Language · Introduction

9

Introduction · How to Use This Book

Chapters 4–9: ownership, types, modules, errors

Table 0.1: What Chapters 4–9 cover

ChapterWhat you’ll learn
Chapter 4Rust’s ownership system
Chapter 5Structs and methods
Chapter 6Enums, match expressions, and the if let and let...else control flow constructs
Chapter 7Rust’s module system and the privacy rules for organizing your code and its public application programming interface (API)
Chapter 8Common collection data structures that the standard library provides: vectors, strings, and hash maps
Chapter 9Rust’s error-handling philosophy and techniques

You’ll use structs and enums (Chapters 5 and 6) to make custom types.

The Rust Programming Language · Introduction

10

Introduction · How to Use This Book

Chapters 10–15: abstraction, testing, a project, Cargo

Table 0.2: What Chapters 10–15 cover

ChapterWhat you’ll learn
Chapter 10Generics, traits, and lifetimes, which give you the power to define code that applies to multiple types
Chapter 11Testing, which even with Rust’s safety guarantees is necessary to ensure that your program’s logic is correct
Chapter 12 (project)Building our own implementation of a subset of functionality from the grep command line tool, which searches for text within files, using many of the concepts from the previous chapters
Chapter 13Closures and iterators: features of Rust that come from functional programming languages
Chapter 14Cargo in more depth, and best practices for sharing your libraries with others
Chapter 15Smart pointers that the standard library provides, and the traits that enable their functionality

The Rust Programming Language · Introduction

11

Introduction · How to Use This Book

Chapters 16–21: concurrency, idioms, advanced topics

Table 0.3: What Chapters 16–21 cover

ChapterWhat you’ll learn
Chapter 16Different models of concurrent programming, and how Rust helps you program in multiple threads fearlessly
Chapter 17Building on that: Rust’s async and await syntax, along with tasks, futures, and streams, and the lightweight concurrency model they enable
Chapter 18How Rust idioms compare to object-oriented programming principles you might be familiar with
Chapter 19A reference on patterns and pattern matching, which are powerful ways of expressing ideas throughout Rust programs
Chapter 20A smorgasbord of advanced topics of interest, including unsafe Rust, macros, and more about lifetimes, traits, types, functions, and closures
Chapter 21 (project)Completing a project in which we implement a low-level multithreaded web server!

The Rust Programming Language · Introduction

12

Introduction · How to Use This Book

The appendixes, and no wrong way to read

Finally, some appendixes contain useful information about the language in a more reference-like format.

Table 0.4: The appendixes

AppendixContents
ARust’s keywords
BRust’s operators and symbols
CDerivable traits provided by the standard library
DSome useful development tools
ERust editions
FTranslations of the book
GHow Rust is made and what nightly Rust is

There is no wrong way to read this book: If you want to skip ahead, go for it! You might have to jump back to earlier chapters if you experience any confusion. But do whatever works for you.

The Rust Programming Language · Introduction

13

Introduction · How to Use This Book

Reading compiler errors, and Ferris

An important part of learning Rust is learning how to read the error messages the compiler displays: these will guide you toward working code. So the book provides many examples that don’t compile, along with the error message the compiler shows in each situation.

If you enter and run a random example, it may not compile! Read the surrounding text to see whether the example is meant to error. In most situations, the book leads you to the correct version of any code that doesn’t compile. Ferris, the crab mascot, also marks code that isn’t meant to work:

Table 0.5: Ferris icons beside code listings

FerrisMeaning
Ferris with a question markThis code does not compile!
Ferris throwing up their handsThis code panics!
Ferris with one claw up, shruggingThis code does not produce the desired behavior.

The Rust Programming Language · Introduction

14

Introduction · Source Code

Where the book’s source lives

The source files from which this book is generated can be found on GitHub:

https://github.com/rust-lang/book/tree/main/src

The Rust Programming Language · Introduction

15

Introduction · Summary

Chapter summary

The introduction sets out what Rust is for and how to approach the book:

  • Rust pairs low-level control (such as memory usage) with high-level ergonomics, aiming for safety and productivity, speed and ergonomics.
  • It serves teams (the compiler rejects subtle bugs; Cargo, rustfmt and the Rust Language Server help), students, companies, open source contributors, and anyone who values speed and stability.
  • Zero-cost abstractions let safe, high-level code run as fast as hand-written low-level code.
  • The book assumes you already program in some language; read it front to back, though skipping ahead is fine.
  • Concept chapters teach an aspect of Rust; Chapters 2, 12 and 21 are projects.
  • Many examples deliberately fail to compile: read the compiler’s errors, and watch for Ferris’s icons.

Remark. Next, Chapter 1 installs Rust, writes a “Hello, world!” program, and introduces Cargo, Rust’s package manager and build tool.

The Rust Programming Language · Introduction

16