Repo Structure
Folders and Key Files |
Book Organization |
Code Organization |
All Examples are Fully and Continuously Tested |
Folders and Key Files
The book's GitHub repository is structured as follows:
- The Dev Container and Docker (Compose) configuration files are found in
.devcontainer
⮳. Review the Dev Container and Docker page for usage. - The
.github
folder contains the GitHub configuration, including the CD/CI workflows that build the code & book and deploy the book to Github Pages. - The
.vscode
folder contains the VS Code configuration (other editors can be used). - The
bin
folder stores executables used to generate parts of the book or used by the book building process. Generate these tools usingjust tools release
orjust scrub release
. These commands build the code and copy the compiled executables intobin
. - The
bk
folder contains the book itself:- The
mdbook
⮳ configuration is inbk/book.toml
⮳. - The markdown sources of the book are in the
bk/src
folder (work-in-progress chapters are inbk/drafts
; stub chapters are inbk/later
), the structure of which is described below. - After the book is built using
mdbook
⮳, the resulting HTML and Javascript are found inbk/book/html
. - The templates and assets are in
theme
⮳ andstatic
⮳ respectively. - The Rust examples embedded in the book are found below
bk/crates
(see below for details). - The
bk/master
folder contains the master list of crates used in the book (which is used by a few scripts to generate tables). bk/scripts
contains just⮳ modules (mod.just
files) and shell scripts (*.sh
files) for building the code & book and managing references, links, recipe tables, examples, etc.
- The
- Additional code and tools is found in the
mdbook-scrub
,playground
,publish
,tools
andxmpl
folders.mdbook-scrub
is a custommdbook
preprocessor used by the book.- The
playground
folder contains bits and pieces of code for testing and exploration. Use it to develop new code examples. - The
publish
folder contains a placeholder crate that is published tocrates.io
, so that the links to the book could be found there. tools
contains several command-line utilities to build specific sections of the book, for example book indices.- Additional examples that are too long or complex to be inserted in the book itself are in folders under
xmpl
.
Book Organization
Within the bk/src
folder,
- The main table of contents (
SUMMARY.md
) points to the book chapters, which are grouped by section (language
,links
...) and, below thecategories
directory, bycrates.io
categories⮳.- The
code_organization
section describes how to structure your Rust code using modules, crates, packages and workspaces. - The
contributing
section contains information on how to contribute to the book or its examples (including this file). - The
crate_selection
section lists the crates used in this book and provide recommendations on how to select external Rust crates for your project. - The
indices
folder stores the index of examples and the crate indexes. - The
language
section describes Rust language features: types, functions, control flow, etc. - The
links
section contains links to Rust documentation, books, videos, cheatsheets, etc. standard_library
describes core Standard Library types, such asOption
andResult
.
- The
Each section or category has one or more chapters. Each chapter consists of a main Markdown file (e.g., chapter_name.md
) and an associated include file (e.g., chapter_name.incl.md
) for the 'recipe table', a table of contents with links to all recipes (chapter sections) and associated crates and categories. The main file includes the recipe table file, as well as local refs.incl.md
and global refs/link-refs.md
files, which contain link reference definitions⮳ (the URLs for links).
Each recipe in a given chapter contains a short description of the crates used, links thereto, and includes⮳ one or more code examples (*.rs
files) in a bk/crates
subfolder.
Each book section or category has an introductory index.md
file that lists its recipes by topic / chapter.
In the bk/src/refs
folder, you will find category-refs.md
for link references to the crates.io
category pages; crate-refs.md
for links to crates.io
, lib.rs
and GitHub for each crate used in the book; and other-refs.md
for links to blogs, books, company websites related to Rust.
The bk/drafts
and bk/later
folders follow the same basic organization.
Code Organization
The book's GitHub repository⮳ is a "monorepo" with multiple independent projects:
- The Rust examples embedded in the book use a very large number of dependencies, therefore their compile time is quite long. For that reason, they are in an isolated
cargo
⮳ workspace, which manifest is inbk/crates/Cargo.toml
. The workspace consists of multiple crates belowbk/crates
, each named after sections of the book (e.g.,bk/crates/language
) or, within thebk/crates/cats
folder, aftercrates.io
categories (e.g.,bk/crates/cats/algorithms
).- Each crate in the workspace contains a
Cargo.toml
⮳ file, which list the dependencies used by its code examples. Usecargo add <crate>
orcargo add <crate> -F <feature>
while in the appropriate crate folder, in order to add more as required. - The examples themselves are stored within the crate's
tests
folder in a subfolder named after their chapter, e.g.,bk/crates/<section or cats/some_category>/examples/<chapter_name>/<example_name>.rs
. In a few cases, you may also find examples in the crate'stests
orsrc
folders or within itsbuild.rs
file. - Each example are in a separate
.rs
Rust file that is a module in amain.rs
file. All examples of a given chapter are therefore compiled together. - The book's code examples, being in a
cargo
workspace⮳, share the same dependency versions (a singleCargo.lock
file) and one sharedtarget
directory, avoid unnecessary dependency rebuilding.
- Each crate in the workspace contains a
- The rest of the code is either in standalone crates (
mdbook-scrub
,publish
) or independent workspaces (playground
,tools
andxmpl
).- The
tools
workspace builds a few CLI binaries that share a commontool_lib
library. - In the base folder of each project, type
just
at the shell prompt for a list of commands you can use to format, check, build, lint and test the code.just release
builds tools in release mode and copies the binaries into thebin
folder (in the repo root).
- The
Note that, instead of storing the cargo
cache and compiler outputs in a separate target
folder for each workspace or standalone crate, there is a common target
folder in the repository root. For example, the compiled examples for the book are in target/bk
. This is configured in .cargo/config.toml
files.
Consult the README.md
files of each crate for more details.
All Examples are Fully and Continuously Tested
In order to make sure that all bk/crates
code examples work, they are backed by tests, similar to the following:
// ANCHOR: example
// The portion between ANCHOR and ANCHOR_END is shown in the book.
fn main() {
// Actual book example goes here.
}
// ANCHOR_END: example
// This test is executed by `cargo test` or `cargo nextest run`
// every time the code is built. It calls `main()`.
#[test]
fn test() {
main();
}
These tests are run by the CD/CI workflows (in the .github
folder) following a git push
or pull request.
For the sake of readability, the test boilerplate is hidden in the book.