liveBook · Manning

Crate

As explained in Code Like a Pro in Rust

A crate is a Rust package containing source code and metadata that can be compiled and distributed as a library or application.

A crate is a Rust package containing an application or library. Cargo builds crates, resolves their dependencies, and can publish them to crates.io. Use a library crate when code needs to be consumed by another crate; keep dependencies declared in Cargo.toml. section 2.2 section 2.13

Declare a dependency

Add `rand = "0.8"` under `[dependencies]` in Cargo.toml, or run `cargo add rand`. Cargo interprets a bare version using a caret requirement, so choose version constraints deliberately. section 2.2

Choose crate boundaries for the job:

  • For a larger project, split code into workspace members. Declare a local dependency with a path and list its directory under `[workspace] members`; membership alone does not declare the dependency. Members share a top-level Cargo.lock and target directory. section 2.10
  • For integration tests, put tests under `tests/`. Cargo treats them as separate crates, so they can exercise exported interfaces but cannot call private functions; unit tests can cover those internals. section 7.1
  • For an upstream fix, point a dependency at a local path or Git fork while pursuing an upstream change. Track upstream changes: a fork can become a lasting maintenance obligation. section 2.4

Before `cargo publish`, provide the required license and project details and ensure dependencies are available on crates.io. For private crates, private Git repositories and tags are recommended over the limited private-registry support described here. section 2.5

How Code Like a Pro in Rust covers it

How deep it goes
Works through dependency declarations, workspace membership, patching, publishing, and test boundaries without detailing crate compilation internals.
Examples
Uses a rand manifest entry, a library in a workspace, a patched num_cpus dependency, and quicksort integration tests.
What it assumes
Assumes basic Rust and Cargo.toml familiarity while demonstrating dependency and workspace configuration directly.
What you can do after
Operate/configure it with Cargo to declare dependencies, group workspace members, test public APIs, patch dependencies, and publish eligible crates.
How it connects
Connects crates to Cargo, crates.io, semantic versioning, workspaces, modules, and integration testing.

Also explained in

Compare how 2 books explain crate →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Code Like a Pro in Rust. The section links point to the parts of the book it draws on.