<olclass="chapter"><liclass="expanded affix "><ahref="title-page.html">The Rust Programming Language</a></li><liclass="expanded affix "><ahref="foreword.html">Foreword</a></li><liclass="expanded affix "><ahref="ch00-00-introduction.html"class="active">Introduction</a></li><liclass="expanded "><ahref="ch01-00-getting-started.html"><strongaria-hidden="true">1.</strong> Getting Started</a></li><li><olclass="section"><liclass="expanded "><ahref="ch01-01-installation.html"><strongaria-hidden="true">1.1.</strong> Installation</a></li><liclass="expanded "><ahref="ch01-02-hello-world.html"><strongaria-hidden="true">1.2.</strong> Hello, World!</a></li><liclass="expanded "><ahref="ch01-03-hello-cargo.html"><strongaria-hidden="true">1.3.</strong> Hello, Cargo!</a></li></ol></li><liclass="expanded "><ahref="ch02-00-guessing-game-tutorial.html"><strongaria-hidden="true">2.</strong> Programming a Guessing Game</a></li><liclass="expanded "><ahref="ch03-00-common-programming-concepts.html"><strongaria-hidden="true">3.</strong> Common Programming Concepts</a></li><li><olclass="section"><liclass="expanded "><ahref="ch03-01-variables-and-mutability.html"><strongaria-hidden="true">3.1.</strong> Variables and Mutability</a></li><liclass="expanded "><ahref="ch03-02-data-types.html"><strongaria-hidden="true">3.2.</strong> Data Types</a></li><liclass="expanded "><ahref="ch03-03-how-functions-work.html"><strongaria-hidden="true">3.3.</strong> Functions</a></li><liclass="expanded "><ahref="ch03-04-comments.html"><strongaria-hidden="true">3.4.</strong> Comments</a></li><liclass="expanded "><ahref="ch03-05-control-flow.html"><strongaria-hidden="true">3.5.</strong> Control Flow</a></li></ol></li><liclass="expanded "><ahref="ch04-00-understanding-ownership.html"><strongaria-hidden="true">4.</strong> Understanding Ownership</a></li><li><olclass="section"><liclass="expanded "><ahref="ch04-01-what-is-ownership.html"><strongaria-hidden="true">4.1.</strong> What is Ownership?</a></li><liclass="expanded "><ahref="ch04-02-references-and-borrowing.html"><strongaria-hidden="true">4.2.</strong> References and Borrowing</a></li><liclass="expanded "><ahref="ch04-03-slices.html"><strongaria-hidden="true">4.3.</strong> The Slice Type</a></li></ol></li><liclass="expanded "><ahref="ch05-00-structs.html"><strongaria-hidden="true">5.</strong> Using Structs to Structure Related Data</a></li><li><olclass="section"><liclass="expanded "><ahref="ch05-01-defining-structs.html"><strongaria-hidden="true">5.1.</strong> Defining and Instantiating Structs</a></li><liclass="expanded "><ahref="ch05-02-example-structs.html"><strongaria-hidden="true">5.2.</strong> An Example Program Using Structs</a></li><liclass="expanded "><ahref="ch05-03-method-syntax.html"><strongaria-hidden="true">5.3.</strong> Method Syntax</a></li></ol></li><liclass="expanded "><ahref="ch06-00-enums.html"><strongaria-hidden="true">6.</strong> Enums and Pattern Matching</a></li><li><olclass="section"><liclass="expanded "><ahref="ch06-01-defining-an-enum.html"><strongaria-hidden="true">6.1.</strong> Defining an Enum</a></li><liclass="expanded "><ahref="ch06-02-match.html"><strongaria-hidden="true">6.2.</strong> The match Control Flow Operator</a></li><liclass="expanded "><ahref="ch06-03-if-let.html"><strongaria-hidden="true">6.3.</strong> Concise Control Flow with if let</a></li></ol></li><liclass="expanded "><ahref="ch07-00-managing-growing-projects-with-packages-crates-and-modules.html"><strongaria-hidden="true">7.</strong> Managing Growing Projects with Packages, Crates, and Modules</a></li><li><olclass="section"><liclass="expanded "><ahref="ch07-01-packages-and-crates.html"><strongaria-hidden="true">7.1.</strong> Packages and Crates</a></li><liclass="expanded "><ahref="ch07-02-defining-modules-to-control-scope-and-privacy.html"><strongaria-hidden="true">7.2.</strong> Defining Modules to Control Scope and Privacy</a></li><liclass="expanded "><ahref="ch07-03-paths-for-referring-to-an-item-in-the-modu
<buttonid="sidebar-toggle"class="icon-button"type="button"title="Toggle Table of Contents"aria-label="Toggle Table of Contents"aria-controls="sidebar">
<ahref="print.html"title="Print this book"aria-label="Print this book">
<iid="print-button"class="fa fa-print"></i>
</a>
</div>
</div>
</div>
<divid="search-wrapper"class="hidden">
<formid="searchbar-outer"class="searchbar-outer">
<inputtype="search"name="search"id="searchbar"name="searchbar"placeholder="Search this book ..."aria-controls="searchresults-outer"aria-describedby="searchresults-header">
<p>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.</p>
<h3><aclass="header"href="#people-who-value-speed-and-stability"id="people-who-value-speed-and-stability">People Who Value Speed and Stability</a></h3>
<p>Rust is for people who crave speed and stability in a language. By speed, we
mean the speed of the programs that you can create with Rust and the speed at
which Rust lets you write them. 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. By striving for zero-cost abstractions, higher-level features
that compile to lower-level code as fast as code written manually, Rust
endeavors to make safe code be fast code as well.</p>
<p>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 <em>and</em> productivity, speed <em>and</em> ergonomics. Give
Rust a try and see if its choices work for you.</p>
<h2><aclass="header"href="#who-this-book-is-for"id="who-this-book-is-for">Who This Book Is For</a></h2>
<p>This book assumes that you’ve written code in another programming language but
doesn’t make any assumptions about which one. We’ve tried to make the material
broadly accessible to those from a wide variety of programming backgrounds. We
don’t spend a lot of time talking about what programming <em>is</em> or how to think
about it. If you’re entirely new to programming, you would be better served by
reading a book that specifically provides an introduction to programming.</p>
<h2><aclass="header"href="#how-to-use-this-book"id="how-to-use-this-book">How to Use This Book</a></h2>
<p>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 topic; we typically revisit the
topic in a later chapter.</p>
<p>You’ll find two kinds of chapters in this book: concept chapters and project
chapters. In concept chapters, you’ll learn about an aspect of Rust. In project
chapters, we’ll build small programs together, applying what you’ve learned so
far. Chapters 2, 12, and 20 are project chapters; the rest are concept chapters.</p>
<p>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. Chapter 2 is a
hands-on introduction to the Rust language. Here we cover concepts at a high
level, and later chapters will provide additional detail. If you want to get
your hands dirty right away, Chapter 2 is the place for that. At first, you
might even want to skip Chapter 3, which covers Rust features similar to those
of other programming languages, and head straight to Chapter 4 to learn about
Rust’s ownership system. However, if you’re a particularly meticulous learner
who prefers to learn every detail before moving on to the next, you might want
to skip Chapter 2 and go straight to Chapter 3, returning to Chapter 2 when
you’d like to work on a project applying the details you’ve learned.</p>
<p>Chapter 5 discusses structs and methods, and Chapter 6 covers enums, <code>match</code>
expressions, and the <code>if let</code> control flow construct. You’ll use structs and
enums to make custom types in Rust.</p>
<p>In Chapter 7, you’ll learn about Rust’s module system and about privacy rules
for organizing your code and its public Application Programming Interface
(API). Chapter 8 discusses some common collection data structures that the
standard library provides, such as vectors, strings, and hash maps. Chapter 9
explores Rust’s error-handling philosophy and techniques.</p>
<p>Chapter 10 digs into generics, traits, and lifetimes, which give you the power
to define code that applies to multiple types. Chapter 11 is all about testing,
which even with Rust’s safety guarantees is necessary to ensure your program’s
logic is correct. In Chapter 12, we’ll build our own implementation of a subset
of functionality from the <code>grep</code> command line tool that searches for text
within files. For this, we’ll use many of the concepts we discussed in the
previous chapters.</p>
<p>Chapter 13 explores closures and iterators: features of Rust that come from
functional programming languages. In Chapter 14, we’ll examine Cargo in more
depth and talk about best practices for sharing your libraries with others.
Chapter 15 discusses smart pointers that the standard library provides and the
traits that enable their functionality.</p>
<p>In Chapter 16, we’ll walk through different models of concurrent programming
and talk about how Rust helps you to program in multiple threads fearlessly.
Chapter 17 looks at how Rust idioms compare to object-oriented programming
principles you might be familiar with.</p>
<p>Chapter 18 is a reference on patterns and pattern matching, which are powerful
ways of expressing ideas throughout Rust programs. Chapter 19 contains a
smorgasbord of advanced topics of interest, including unsafe Rust, macros, and
more about lifetimes, traits, types, functions, and closures.</p>
<p>In Chapter 20, we’ll complete a project in which we’ll implement a low-level
multithreaded web server!</p>
<p>Finally, some appendixes contain useful information about the language in a
more reference-like format. Appendix A covers Rust’s keywords, Appendix B
covers Rust’s operators and symbols, Appendix C covers derivable traits
provided by the standard library, Appendix D covers some useful development
tools, and Appendix E explains Rust editions.</p>
<p>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.</p>
<p><spanid="ferris"></span></p>
<p>An important part of the process of learning Rust is learning how to read the
error messages the compiler displays: these will guide you toward working code.
As such, we’ll provide many examples that don’t compile along with the error
message the compiler will show you in each situation. Know that if you enter
and run a random example, it may not compile! Make sure you read the
surrounding text to see whether the example you’re trying to run is meant to
error. Ferris will also help you distinguish code that isn’t meant to work:</p>