Skip to content
Select theme

Cargo and project layout

Cargo is the part of Rust that most languages get wrong. This chapter is the model underneath it: what a crate is, how a module tree is built from files, what pub actually controls.

Then the practical half — pinning versions, features, workspaces, and what to check before you publish.

Coming up (30)
  • What is a crate?the unit the compiler actually builds
  • What is the difference between a crate and a module?compilation unit versus namespace
  • What is the difference between a binary and a library crate?main.rs versus lib.rs
  • How to structure a project with main.rs and lib.rsa thin binary over a testable library
  • How to split a module into multiple filesmod.rs versus the 2018 file layout
  • What is the difference between inline and file modules?mod foo { } versus mod foo;
  • How to fix “file not found for module”the path the compiler inferred from mod
  • What does pub do?visibility, and the default that is not pub
  • What is the difference between pub, pub(crate), and pub(super)?three scopes, three meanings
  • How to re-export items with pub usethe public API you did not write twice
  • How to fix “function is private”visibility, along the whole path
  • How to add a dependencycargo add, and editing the file by hand
  • How to pin a dependency versioncaret, tilde, and =
  • What is Cargo.lock, and should you commit it?binaries yes, libraries no
  • How to use dev-dependencies and build-dependenciestests and build scripts get their own list
  • What are Cargo features?compile-time switches, and feature unification
  • How to create a workspaceone lock file, one target directory
  • How to use path dependenciestwo crates, one checkout
  • How to use a git dependencyrev, tag, or branch
  • How to vendor dependencies for offline buildscargo vendor and the config file
  • What is build.rs?code generation, and linking native code
  • How to use conditional compilation#[cfg(target_os)] and cfg! at runtime
  • How to write platform-specific codeone trait, several implementations
  • How to add examples to a projectthe examples/ directory, and cargo run –example
  • How to publish a crate to crates.iothe metadata audit before the first push
  • How to version a crate with semverwhat a breaking change is, precisely
  • How to keep dependencies up to datecargo update, cargo outdated, and reading the diff
  • How to choose a cratethe checklist before you add a dependency
  • Which crates should you learn first?the small set almost every project pulls in
  • How to move a project to a newer editionedit Cargo.toml, then fix what stops compiling