Publishing, Consuming & Yanking
A crate becomes reusable when it's on a registry. crates.io is the public one; companies run private ones (Artifactory, Cloudsmith, Kellnr, a sparse index on any web server) for internal crates. The commands are the same for both. Sessions here use the course machine's private registry local, started with crate-registry start; its address is configured in ~/.cargo/config.toml and its token in ~/.cargo/credentials.toml.
How registries are configured
# .cargo/config.toml (or ~/.cargo/config.toml)
[registries.company]
index = "sparse+https://crates.example.com/api/v1/crates/"
[registry]
default = "company" # optional: make `cargo publish` use it without --registry
global-credential-providers = ["cargo:token"]The token goes in ~/.cargo/credentials.toml (written by cargo login --registry company), in an environment variable (CARGO_REGISTRIES_COMPANY_TOKEN, CARGO_REGISTRY_TOKEN for crates.io) or, better, comes from a credential provider such as the OS keychain. For crates.io from GitHub Actions, trusted publishing exchanges the workflow's OIDC identity for a short-lived token, so no long-lived secret is stored at all.
Publish, then consume
crate-registry start
cargo new --lib greet --vcs none -q
cd greet
cat > Cargo.toml <<'T'
[package]
name = "greet"
version = "0.1.0"
edition = "2024"
description = "Friendly greetings"
license = "MIT"
repository = "https://example.com/greet"
publish = ["local"]
[dependencies]
T
echo 'pub fn hello(name: &str) -> String { format!("Hello, {name}!") }' > src/lib.rs
cargo publish 2>&1
echo "--- what the registry knows"
cargo info greet --registry local 2>&1 | grep -v Updating
echo "--- a consumer"
cd ..
cargo new app --vcs none -q
cd app
cargo add greet --registry local 2>&1
echo 'fn main() { println!("{}", greet::hello("registry")); }' > src/main.rs
cargo run -q
grep -A2 'name = "greet"' Cargo.locknote: found `local` as only allowed registry. Publishing to it automatically.
Updating `local` index
Packaging greet v0.1.0 (/home/learner/greet)
Packaged 4 files, 1.2KiB (793B compressed)
Verifying greet v0.1.0 (/home/learner/greet)
Compiling greet v0.1.0 (/home/learner/greet/target/package/greet-0.1.0)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.12s
Uploading greet v0.1.0 (/home/learner/greet)
Uploaded greet v0.1.0 to registry `local`
note: waiting for greet v0.1.0 to be available at registry `local`
help: you may press ctrl-c to skip waiting; the crate should be available shortly
Published greet v0.1.0 at registry `local`
--- what the registry knows
Downloading crates ...
Downloaded greet v0.1.0 (registry `local`)
greet
Friendly greetings
version: 0.1.0 (from registry `local`)
license: MIT
rust-version: unknown
repository: https://example.com/greet
--- a consumer
Updating `local` index
Adding greet v0.1.0 to dependencies
Updating `local` index
Locking 1 package to highest Rust 1.99.1 compatible version
Hello, registry!
name = "greet"
version = "0.1.0"
source = "sparse+http://127.0.0.1:8282/index/"
publish = ["local"]both restricts where the crate may go and makeslocalthe default forcargo publish.cargo publish=cargo package(checks, tarball, verify build) + upload + wait until the index shows the new version.- In the consumer, the dependency records
registry = "local", andCargo.lockrecords the registry's index as thesource. Anyone building the app needs the same[registries.local]entry in their config.
A version is forever
Once published, a version's content is immutable: you can't overwrite it, and on crates.io you can't delete it. The only tool is a new version, plus yanking the bad one.
crate-registry start
cargo new --lib greet --vcs none -q
cd greet
sed -i 's/^edition.*/&\ndescription = "Friendly greetings"\nlicense = "MIT"\nrepository = "https:\/\/example.com\/greet"\npublish = ["local"]/' Cargo.toml
release() { sed -i "s/^version = .*/version = \"$1\"/" Cargo.toml; echo "pub fn hello() -> &'static str { \"$2\" }" > src/lib.rs; cargo publish --no-verify -q 2>&1; }
release 0.1.0 "hello from 0.1.0"
release 0.1.1 "hello from 0.1.1 (has a bug)"
echo "--- publishing 0.1.1 again"
release 0.1.1 "sneaky fix"
cd ..
cargo new app --vcs none -q
cd app
echo 'greet = { version = "0.1", registry = "local" }' >> Cargo.toml
echo 'fn main() { println!("{}", greet::hello()); }' > src/main.rs
cargo run -q
echo "--- the author yanks 0.1.1"
cargo yank greet@0.1.1 --registry local 2>&1
echo "--- the existing lockfile keeps working"
cargo run -q
echo "--- a fresh resolution skips the yanked version"
rm Cargo.lock
cargo run -q 2>&1 | grep -v Updating
echo "--- asking for exactly the yanked version"
sed -i 's/version = "0.1"/version = "=0.1.1"/' Cargo.toml
rm Cargo.lock
cargo build -q 2>&1--- publishing 0.1.1 again
error: crate greet@0.1.1 already exists on `local` index
hello from 0.1.1 (has a bug)
--- the author yanks 0.1.1
Updating `local` index
Yank greet@0.1.1
--- the existing lockfile keeps working
hello from 0.1.1 (has a bug)
--- a fresh resolution skips the yanked version
hello from 0.1.0
--- asking for exactly the yanked version
error: failed to select a version for the requirement `greet = "=0.1.1"`
version 0.1.1 is yanked
location searched: `local` index
required by package `app v0.1.0 (/home/learner/app)`
Yanking doesn't delete anything. It means "don't choose this version for new resolutions": projects that already have it locked keep building (so a yank never breaks someone's existing build), but nobody new picks it up. Yank for broken or dangerous releases, then publish a fixed version (0.1.2). If a secret leaked into a published crate, yanking doesn't help: rotate the secret, because the content stays downloadable.
cargo yank --undo greet@0.1.1 reverses a yank. Ownership of crates.io crates is managed with cargo owner --add/--remove (users or GitHub teams).
Getting SemVer right before publishing
Users rely on caret requirements, so a minor or patch release must not break them. What counts as breaking in Rust is subtle: removing a public item, adding a required trait method, adding a field to a public struct without #[non_exhaustive], changing a function's generic bounds, even making a type stop being Send. The Cargo Book has a full chapter, "SemVer Compatibility", with an example for every rule.
cargo-semver-checks automates this. It builds rustdoc JSON for your crate and for the last published version and runs a large set of lints over the difference:
cargo install --locked cargo-semver-checks
cargo semver-checks # compare with the latest version on crates.io
cargo semver-checks --baseline-rev v0.1.0 # or with a Git revisionIt isn't installed on this course's machine (and its default baseline comes from crates.io), so it isn't run here. Put it in the release workflow, right before cargo publish; Release Engineering builds the whole pipeline around steps like this.
A publishing checklist
versionbumped according to SemVer (andcargo semver-checksagrees).cargo package --listreviewed; metadata complete;includeset.cargo publish --dry-runpasses (all of publish except the upload).- Changelog updated, Git tag created for the same commit (
.cargo_vcs_info.jsonrecords it). - In a workspace, publish dependencies before dependents (Cargo 1.90+ can publish several workspace packages in one
cargo publish --workspace, in dependency order).
Test yourself
What does yanking version 0.1.1 do?
- Deletes it from the registry
- Stops new resolutions from choosing it; existing lockfiles that pin it keep working
- Breaks every build that uses it
- Replaces it with the previous version
You published 0.1.1 with a bug. Can you publish 0.1.1 again with the fix?
- Yes, with --force
- No: versions are immutable; publish 0.1.2 (and yank 0.1.1)
- Yes, after yanking
- Only on private registries
What does publish = ["local"] in Cargo.toml do?
- Publishes on every build
- Allows publishing only to the
localregistry and makes it the default target for cargo publish - Marks the crate as private
- Sets the index URL