This repository contains complete code for the larger example programs from the book “Programming Rust”, by Jim Blandy, Jason Orendorff, and Leonora Tindall.
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 cargo test.
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.
-
The
gcddirectory holds the command-line program for computing the greatest common denominator of a list of numbers. -
The
actix-gcddirectory holds the code for the simple web service, implemented using theactix-webframework, that computes greatest common denominators.Note that the code shown here has been updated to use a newer version of
actix-webthan shown in the book, to address security holes in earlier versions of the framework. -
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. Thesingle-threadedbranch holds the code for the single-threaded version, and thebandsbranch 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.
- 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
fern_simsubdirectory.
-
The
queuedirectory holds a library that defines theQueuetype, representing a queue ofcharvalues. -
The
generic-queuedirectory holds code for genericQueuetype.
- The
binary-treedirectory holds the source code for theBinaryTreetype that appears in the “Generic Enums” and “Populating a Binary Tree” sections.
-
The
complexdirectory holds theComplextype used as a running example throughout the chapter. -
The
intervaldirectory holds theIntervaltype for which the book implements thestd::cmp::PartialOrdtrait.
- The 'basic-router' directory holds the
BasicRoutertype used as an example in the “Callbacks” section.
- The
binary-treedirectory holds the implementation of theIteratortrait for theBinaryTreetype originally defined in the “Enums and Patterns” chapter.
- The
complexdirectory includes the implementation of thestd::fmt::Displayformatting trait for a complex number type, shown in the section “Formatting Your Own Types”.
-
The
grepdirectory holds the simple grep-like program shown in the section “Reading Lines”. -
The
copydirectory holds the program for copying directory trees from the section “Reading Directories”, including the additions shown in the next section, “Platform-Specific Features”. -
The
echo-serverdirectory holds the simple network service shown in the “Networking” section. -
The
http-getdirectory holds the command-line program that uses thereqwestcrate to carry out an HTTP request.
-
The search engine used as a running example throughout the book has its own repository, at
https://github.com/ProgrammingRust/fingertips. -
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.
-
The
cheapo-requestdirectory usesasync-std's asynchronous networking functions and theblock_onexecutor function to make a bare-bones HTTP request. -
The
many-requestsdirectory makes several requests concurrently on a single thread, usingasync-std'sspawn_local. -
The
many-requests-surfdirectory does the same usingsurf, an open-source asynchronous HTTP client library available from crates.io that does a much better job with HTTP than our chapter'scheapo_requestfunction. -
The
spawn-blockingdirectory includes the definition ofspawn_blockingpresented in the section "Primitive Futures and Executors: When Is A Future Worth Polling Again?". The crate also includes some tests to exercise the function. -
The
block-ondirectory includes the implementation of a simpleblock_onexecutor, which demonstrates polling and waiting for wakers to be invoked. -
The chat client and server used as an extended example are in their own repository, at
https://github.com/ProgrammingRust/async-chat.
- The
json-macrodirectory holds the definition of thejson!macro built in the section “The json! Macro”.
-
The
asciidirectory holds theAsciitype used as an example in the sections “Unsafe Blocks” and “Unsafe Functions”. -
The
ref-with-flagdirectory holds theRefWithFlagtype from the “Raw Pointers” section. -
The
gap-bufferdirectory holds theGapBuffertype, used in the “Raw Pointers” section to illustrate pointer arithmetic andstd::ptr::readandstd::ptr::write.
-
The
libgit2-rsandlibgit2-rs-safedirectories contain the two versions of the program that uses Rust's foreign function interface to call functions from thelibgit2C library. The version inlibgit2-rsis written as a single giant block of unsafe code, whereas the version inlibgit2-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.