contents

Front matter

foreword

preface

acknowledgments

about this book

about the author

  

  1   Some basics

  1.1   Introducing Rust

A pep talk

Rust is like a critical spouse

  1.2   Comments

  1.3   Primitive types: Integers, characters, and strings

  1.4   Type inference

  1.5   Floats

  1.6   “Hello, World!” and printing

  1.7   Declaring variables and code blocks

  1.8   Display and Debug

  1.9   Smallest and largest numbers

  1.10 Mutability (changing)

  1.11 Shadowing

  2   Memory, variables, and ownership

  2.1   The stack, the heap, pointers, and references

  2.2   Strings

  2.3   const and static

  2.4   More on references

  2.5   Mutable references

Rust’s reference rules

Situation 1: Only one mutable reference

Situation 2: Only immutable references

Situation 3: The problem situation

  2.6   Shadowing again

  2.7   Giving references to functions

  2.8   Copy types

  2.9   Variables without values

  2.10 More about printing

  3   More complex types

  3.1   Collection types

Arrays

Vectors

Tuples

  3.2   Control flow

Basic control flow

Match statements

Loops

  4   Building your own types

  4.1   A quick overview of structs and enums

Structs

Enums

Casting enums into integers

Enums to use multiple types

Implementing structs and enums

  4.2   Destructuring

  4.3   References and the dot operator

  5   Generics, option, and result

  5.1   Generics

  5.2   Option and Result

Option

Result

Some other ways to do pattern matching

  6   More collections, more error handling

  6.1   Other collections

HashMap (and BTreeMap)

HashSet and BTreeSet

BinaryHeap

VecDeque

  6.2   The ? operator

  6.3   When panic and unwrap are good

  7   Traits: Making different types do the same thing

  7.1   Traits: The basics

All you need are the method signatures

More complex examples

Traits as bounds

Traits are like qualifications

  7.2   The From trait

  7.3   The orphan rule

  7.4   Getting around the orphan rule with newtypes

  7.5   Taking a String and a &str in a function

  8   Iterators and closures

  8.1   Chaining methods

  8.2   Iterators

  8.3   Closures and closures inside iterators

Closures inside of methods

Closures: Lazy and fast

|_| in a closure

  9   Iterators and closures again!

  9.1   Helpful methods for closures and iterators

Mapping and filtering

Some more iterator and related methods

Checking and finding items inside iterators

Cycling, zipping, folding, and more

  9.2   The dbg! macro and .inspect

10   Lifetimes and interior mutability

10.1   Types of &str

10.2   Lifetime annotations

Lifetimes in functions

Lifetime annotations in types

The anonymous lifetime

10.3   Interior mutability

Cell

RefCell

Mutex

RwLock

11   Multiple threads and a lot more

11.1   Importing and renaming inside a function

11.2   The todo! macro

11.3   Type aliases

11.4   Cow

11.5   Rc

Why Rc exists

Using Rc in practice

Avoiding lifetime annotations with Rc

11.6   Multiple threads

Spawning threads

Using JoinHandles to wait for threads to finish

Types of closures

Using the move keyword

12   More on closures, generics, and threads

12.1   Closures as arguments

Some simple closures

The relationship between FnOnce, FnMut, and Fn

Closures are all unique

A closure example

12.2   impl Trait

Regular generics compared to impl Trait

Returning closures with impl Trait

12.3   Arc

12.4   Scoped threads

12.5   Channels

Channel basics

Implementing a channel

13   Box and Rust documentation

13.1   Reading Rust documentation

assert_eq!

Searching

The [src] button

Information on traits

Attributes

13.2   Box

Some Box basics

Putting a Box around traits

Using a Box to handle multiple error types

Downcasting to a concrete type

14   Testing and building your code from tests

14.1   Crates and modules

Module basics

More on how the pub keyword works

Modules inside modules

14.2   Testing

Just add #[test], and now it’s a test

What happens when tests fail

Writing multiple tests

14.3   Test-driven development

Building a calculator: Starting with the tests

Putting the calculator together

15   Default, the builder pattern, and Deref

15.1   Implementing Default

15.2   The builder pattern

Writing builder methods

Adding a final check to the builder pattern

Making the builder pattern more rigorous

15.3   Deref and DerefMut

Deref basics

Implementing Deref

Implementing DerefMut

Using Deref the wrong way

16   Const, “unsafe” Rust, and external crates

16.1   Const generics

16.2   Const functions

16.3   Mutable statics

16.4   Unsafe Rust

Overview of unsafe Rust

Using static mut in unsafe Rust

Rust’s most famous unsafe method

Methods ending in _unchecked

16.5   Introducing external crates

Crates and Cargo.toml

Using the rand crate

Rolling some dice with rand

17   Rust’s most popular crates

17.1   serde

17.2   Time in the standard library

17.3   chrono

Checking the code inside external crates

Back to chrono

17.4   Rayon

17.5   Anyhow and thiserror

Anyhow

thiserror

17.6   Blanket trait implementations

17.7   lazy_static and once_cell

Lazy static: Lazily evaluated statics

OnceCell: A cell to only write to once

18   Rust on your computer

18.1   Cargo

Why everyone uses Cargo

Using Cargo and what Rust does while it compiles

18.2   Working with user input

User input through stdin

Accessing command-line arguments

Accessing environment variables

18.3   Using files

Creating files

Opening existing files

Using OpenOptions to work with files

18.4   cargo doc

19   More crates and async Rust

19.1   The reqwest crate

19.2   Feature flags

19.3   Async Rust

Async basics

Checking whether a Future is ready

Using an async run time

Some other details about async Rust

20   A tour of the standard library

20.1   Arrays

Arrays now implement Iterator

Destructuring and mapping arrays

Using from_fn to make arrays

20.2   char

20.3   Integers

Checked operations

The Add trait and other similar traits

20.4   Floats

20.5   Associated items and associated constants

Associated functions

Associated types

Associated consts

20.6   bool

20.7   Vec

20.8   String

20.9   OsString and CString

21   Continuing the tour

21.1   std::mem

21.2   Setting panic hooks

21.3   Viewing backtraces

21.4   The standard library prelude

21.5   Other macros

unreachable!

column!, line!, file!, and module_path!

thread_local!

cfg!

22   Writing your own macros

22.1   Why macros exist

22.2   Writing basic macros

22.3   Reading macros from the standard library

22.4   Using macros to keep your code clean

23   Unfinished projects: Projects for you to finish

23.1   Setup for the last two chapters

23.2   Typing tutor

Setup and first code

Developing the code

Further development and cleanup

Over to you

23.3   Wikipedia article summary searcher

Setup and first code

Developing the code

Further development and cleanup

Over to you

23.4   Terminal stopwatch and clock

Setup and first code

Developing the code

Further development and cleanup

Over to you

24   Unfinished projects, continued

24.1   Web server word-guessing game

Setup and first code

Developing the code

Further development and cleanup

Over to you

24.2   Laser pointer

Setup and first code

Developing the code

Further development and cleanup

Over to you

24.3   Directory and file navigator

Setup and first code

Developing the code

Further development and cleanup

Over to you

  

index