Organize a Rust project into crates, modules and workspaces

8 steps · about 2 h 15 min · Intermediate

  1. Tell packages, crates and modules apart (tutorial, The Rust Book, 10 min)

    Sets out the module system vocabulary that every later step depends on.

  2. Group code into modules and control privacy (tutorial, The Rust Book, 20 min)

    Shows the module tree, pub and how privacy works between parent and child modules.

  3. Move modules into their own files (tutorial, The Rust Book, 10 min)

    Maps the module tree onto files and folders as a crate grows.

  4. Experiment with pub(crate) and pub(super) (interactive, Rust by Example, 15 min)

    Editable examples of every visibility level you can change and run.

  5. Split the project into a workspace (tutorial, The Rust Book, 20 min)

    Builds a workspace with a binary and library crates sharing one lockfile and target directory.

  6. Share dependencies and settings across members (reference, The Cargo Book, 20 min)

    Covers workspace.dependencies, inherited package fields and default members.

  7. Gate optional code behind Cargo features (reference, The Cargo Book, 25 min)

    Explains additive features, optional dependencies and how feature unification works.

  8. Check the exact visibility rules (reference, The Rust Reference, 15 min)

    States precisely what pub(in path), pub(crate) and re-exports allow.