liveBook · Manning

Chapter 2: Language foundations

What you will learn in chapter 2 of Rust in Action: Systems programming concepts and techniques

Introduces the chapter’s plan for learning Rust fundamentals through examples and a grep-lite project.

After this chapter you will be able to

Section by section

Introduction

Introduces the chapter’s plan for learning Rust fundamentals through examples and a grep-lite project.

2.1 Creating a running program

Introduces the idea that Rust source code is text that can be converted into a program a CPU can interpret.

Key terms: plain text file, Rust source code, CPU, running program

2.1.1 Compiling single files with rustc

This section explains how rustc turns a single Rust source file into an executable and shows how to compile one from a shell.

Key terms: rustc, main(), source code, shell, compiler, machine code, executable

2.1.2 Compiling Rust projects with cargo

This section shows how to move a single-file Rust program into a cargo project and run it with cargo.

Key terms: cargo, rustc, dependencies, cargo init, cargo run, target, verbose flag (-v)

2.2 A glance at Rust’s syntax

The section presents Rust’s syntax as largely familiar and predictable, with a few basic conventions.

Key terms: curly brackets, assignment operator, whitespace-agnostic

2.2.1 Defining variables and calling functions

This section uses a short Rust program to introduce variable bindings, type annotations, function syntax, and formatted console output.

Key terms: fn, main(), code blocks, lexical scopes, let, variable bindings, immutable, i32, numeric literals, type annotations

2.3 Numbers

Previews a discussion of creating numeric types and performing operations on them in Rust.

2.3.1 Integers and decimal (floating-point) numbers

Introduces Rust’s basic numeric syntax, its distinct numeric types and explicit conversions, and methods on numbers.

Key terms: integer, floating-point numbers, infix notation, operator overloading, numeric types, size in bytes, explicit conversions, methods, type suffix, concrete type

2.3.2 Integers with base 2, base 8, and base 16 notation

The section introduces Rust’s binary, octal, and hexadecimal integer notation, then explains numeric type families and how integer width affects range and storage.

Key terms: numeric literals, binary, octal, hexadecimal, 0b, 0o, signed integers, unsigned integers, floating-point types, i

2.3.3 Comparing numbers

This section explains how Rust compares numeric values and why type conversions, floating-point approximation, and NaN require care.

Key terms: as, type cast, promotion, demoted, TryInto, try_into(), trait, Result, unwrap(), floating-point

2.3.4 Rational, complex numbers, and other numeric types

This section shows how to use a third-party crate for specialized numeric types and introduces complex numbers and two ways to initialize them.

Key terms: standard library, num crate, crate, Cargo.toml, [dependencies], complex numbers, real, imaginary, re, im

2.4 Flow control

Introduces flow control as a way for Rust programs to depart from ordinary top-to-bottom execution.

Key terms: flow control

2.4.1 For: The central pillar of iteration

This section introduces Rust’s `for` loop, explains how references affect reuse and modification of a collection, and compares direct iteration with manual indexing.

Key terms: for loop, container, item, reference, ampersand (&), mutable reference, mut, anonymous loops, underscore (_), exclusive range

2.4.2 Continue: Skipping the rest of the current iteration

2.4.3 While: Looping until a condition changes its state

This subsection explains how a while loop repeats while a Boolean condition holds and notes that Rust programmers prefer loop for endless repetition.

Key terms: while loop, condition, predicate, while, loop keyword

2.4.4 Loop: The basis for Rust’s looping constructs

Introduces `loop` as a way to repeat a code block until a `break` is encountered or the program is terminated externally.

Key terms: loop, break

2.4.5 Break: Aborting a loop

This subsection explains how break exits a loop and how loop labels let it exit a selected nested loop.

Key terms: break, loop labels, nested loop, goto

2.4.6 If, if else, and else: Conditional branching

This section explains Boolean conditional branching in Rust and shows how conditionals, loops, and other constructs fit its expression-based design.

Key terms: if, if else, else, Boolean, truthy, falsey, expression, return, conditional expressions, match

2.4.7 Match: Type-aware pattern matching

This subsection introduces Rust’s match as a concise way to test multiple values while requiring every possible case to be handled.

Key terms: match, compiler error, inclusive ranges, Boolean OR, underscore, fall through

2.5 Defining functions

The section uses a small add function to introduce Rust’s requirement to specify parameter and return types in a function definition.

Key terms: parameter’s types, return type, i32

2.6 Using references

This section explains how Rust references stand in for values and how dereferencing works in an array-search example.

Key terms: reference, reference operator (&), dereference operator (*), item, haystack, *item

2.7 Project: Rendering the Mandelbrot set

This section walks the reader through creating and running a Rust project that renders the Mandelbrot set in the terminal.

Key terms: Cargo, num crate, Cargo.toml, src/main.rs, cargo run, Mandelbrot set

2.8 Advanced function definitions

This introduction notes that Rust function definitions can be more complex than the earlier example and points readers to the following sections.

2.8.1 Explicit lifetime annotations

This section explains how to read explicit lifetime annotations on reference parameters, why Rust sometimes needs them, and why they appear in function definitions rather than calls.

Key terms: lifetime variables, lifetime annotations, reference, function signature, lifetime system, lifetime elision, lifetime parameters, decoupled, function definition

2.8.2 Generic functions

This section explains how generic functions accept multiple types and how trait bounds restrict those types when a function uses an operator.

Key terms: generic type, type variable, T, trait bound, std::ops::Add, Output = T, trait, operator overloading, syntactic sugar

2.9 Creating grep-lite

This section builds a simple text search example while introducing Rust’s string types and iterator-based line numbering.

Key terms: grep-lite, lines(), contains(), String, str, &str, string slice, owned, borrowed, string literal

2.10 Making lists of things with arrays, slices, and vectors

Introduces the basic tradeoff between arrays and vectors before examining Rust’s list types in more detail.

Key terms: arrays, vectors, fixed-width, growable, bookkeeping

2.10.1 Arrays

This section explains how Rust arrays are created, how their fixed length forms part of their type, and what happens when indexing goes out of bounds.

Key terms: array, [T; n], element type, repeat expression, array indexing, bounds checked, panics

2.10.2 Slices

This section explains how slices differ from arrays, why they are useful, and why they are usually used through references.

Key terms: slice, dynamically sized, array, [T], [T; n], view, reference, &[T], usize, pointer

2.10.3 Vectors

Introduces growable vectors through a grep-lite context-lines example, explaining their tradeoffs, nested types, and a text-encoding caution.

Key terms: Vec<T>, vectors, arrays, two-pass strategy, grep-lite, context, tags, Vec<Vec<(usize, String)>>, tuple, usize

2.11 Including third-party code

Introduces why Rust projects use third-party crates and shows how to add and build the regex crate with Cargo.

Key terms: third-party crates, standard library, regex crate, regular expressions, exact matches, cargo new, cargo add regex@1, dependency, Cargo.toml, cargo build

2.11.1 Adding support for regular expressions

This section adds regular-expression support to grep-lite and shows how to run the updated example, while noting that the example uses little of the new flexibility.

Key terms: regular expressions, patterns, grep-lite, grep-lite/Cargo.toml, regex, grep-lite/src/main.rs, cargo run

2.11.2 Generating the third-party crate documentation locally

This section shows how to generate and view local HTML documentation for a project’s third-party crates.

Key terms: third-party crates, cargo doc, HTML documentation, cargo doc --open

2.11.3 Managing Rust toolchains with rustup

This section explains how rustup manages Rust toolchains and opens local Rust documentation.

Key terms: rustup, cargo, Rust toolchains, nightly, stable, rustup doc, standard library

2.12 Supporting command-line arguments

The section adds command-line argument support to grep-lite with clap and shows how to pass arguments through cargo run.

Key terms: grep-lite, clap, command-line arguments, pattern, cargo run, --, executable binary, usage documentation, --help

2.13 Reading from files

This section introduces reading a file through a buffered reader and using a line iterator instead of manually looping through the file.

Key terms: File, BufReader, buffered I/O, helper iterator

2.14 Reading from stdin

The section introduces a generic function as a way for a command-line utility to read from either a file or stdin without duplicating code in main().

Key terms: stdin, generic function, main()

2.15 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 1: Introducing Rust Chapter 3: Compound data types →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.