Code Examples for Programming Rust
This repository contains complete code for the larger example programs from the book “Programming Rust”, by Jim Blandy and Jason Orendorff.
Each subdirectory is a distinct Rust project, with its own Cargo.toml file. You
should be able to enter each directory and use
cargo build and
For those projects that define programs,
cargo run should run them.
The example code in this directory and its subdirectories is licensed under the terms of the MIT license. See LICENSE-MIT for details.
Chapter 2: A Tour of Rust
gcddirectory holds the command-line program for computing the greatest common denominator of a list of numbers.
iron-gcddirectory holds the code for the simple web service, implemented using the
ironframework, that computes greatest common denominators.
The Mandelbrot plotting program has its own repository, at
https://github.com/ProgrammingRust/mandelbrot. This repository contains several branches, each showing a different implementation strategy. The
single-threadedbranch holds the code for the single-threaded version, and the
bandsbranch holds the multi-threaded version. Chapter 19, “Concurrency”, shows several other approaches, which appear on other branches; see the repository's README.md file for details.
Chapter 8: Crates and Modules
- We did not actually write a fern simulator. Please accept our sincere apology
for this feckless deception. But the skeleton of modules and definitions we
show in the book is in the
Chapter 9: Structs
queuedirectory holds a library that defines the
Queuetype, representing a queue of
generic-queuedirectory holds code for generic
Chapter 10: Enums and Patterns
binary-treedirectory holds the source code for the
BinaryTreetype that appears in the “Generic Enums” and “Populating a Binary Tree” sections.
Chapter 12: Operator Overloading
complexdirectory holds the
Complextype used as a running example throughout the chapter.
intervaldirectory holds the
Intervaltype for which the book implements the
Chapter 14: Closures
- The 'basic-router' directory holds the
BasicRoutertype used as an example in the “Callbacks” section.
Chapter 15: Iterators
binary-treedirectory holds the implementation of the
Iteratortrait for the
BinaryTreetype originally defined in the “Enums and Patterns” chapter.
Chapter 17: Strings and Text
complexdirectory includes the implementation of the
std::fmt::Displayformatting trait for a complex number type, shown in the section “Formatting Your Own Types”.
Chapter 18: Input and Output
grepdirectory holds the simple grep-like program shown in the section “Reading Lines”.
copydirectory holds the program for copying directory trees from the section “Reading Directories”, including the additions shown in the next section, “Platform-Specific Features”.
echo-serverdirectory holds the simple network service shown in the “Networking” section.
http-getdirectory holds the command-line program that uses the
reqwestcrate to carry out an HTTP request.
Chapter 19: Concurrency
The search engine used as a running example throughout the book has its own repository, at
The Mandelbrot set plotter discussed in the section “Revisiting the Mandelbrot Set” also has its own repository, at
https://github.com/ProgrammingRust/mandelbrot. The repository includes several branches exploring different implementations; see the repository's README.md file for details.
Chapter 20: Macros
json-macrodirectory holds the definition of the
json!macro built in the section “The json! Macro”.
Chapter 21: Unsafe Code
asciidirectory holds the
Asciitype used as an example in the sections “Unsafe Blocks” and “Unsafe Functions”.
ref-with-flagdirectory holds the
RefWithFlagtype from the “Raw Pointers” section.
gap-bufferdirectory holds the
GapBuffertype, used in the “Raw Pointers” section to illustrate pointer arithmetic and
libgit2-rs-safedirectories contain the two versions of the program that uses Rust's foreign function interface to call functions from the
libgit2C library. The version in
libgit2-rsis written as a single giant block of unsafe code, whereas the version in
libgit2-rs-safeimplements a safe Rust interface to the same functionality, using Rust's type system to enforce libgit2's rules for proper use of the library.
Note that both of these require you to have a copy of
libgit2present on your system. The chapter provides detailed instructions for building the correct version, for Linux, macOS, and Microsoft Windows.