Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Modules and Crates  ›  Capstone Project

Publishing on crates.io

Publishing on Crates.io 12 minPublishing uploads a permanent, versioned copy of a library crate to crates.io after you add the required metadata, log in with an API token and run cargo publish.

Publishing on crates.io

The library team wrote a tidy little crate that checks shelf codes like F-120. Another library wants to use it. Publishing it on crates.io, Rust's public registry, lets anyone add it with one line, and it comes with one promise you cannot take back.

The rule

Describe it, dry-run it, publish it: every version is permanent.

1. Describe it. crates.io refuses a package without a description and a license. The name must not already be taken; names are first come, first served.

[package]
name = "shelf-codes"
version = "0.1.0"
edition = "2021"
description = "Parse and validate library shelf codes such as F-120"
license = "MIT OR Apache-2.0"
repository = "https://github.com/example/shelf-codes"
readme = "README.md"
keywords = ["library", "parser"]

Write doc comments (///) on public items; docs.rs builds your documentation automatically after publishing.

2. Log in once. Sign in to crates.io, create an API token, then:

cargo login          # paste the token when asked

3. Dry-run, then publish.

cargo publish --dry-run    # builds the package exactly as it would upload
cargo publish

The permanence rule. A version, once published, can never be overwritten. To fix a bug you publish a new version. Follow semantic versioning: patch (0.1.0 → 0.1.1) for fixes, minor for new features, major for breaking changes, remembering that below 1.0 a minor bump counts as breaking.

Yanking a bad version:

cargo yank --version 0.1.0

This stops new lock files from choosing 0.1.0. It does not delete the code, and projects whose Cargo.lock already names 0.1.0 still build. Treat anything you publish, including an accidentally committed secret, as public forever.

The shortcut

Before every publish, run a four-word check: name, metadata, version, dry-run.

Releasing shelf-codes 0.1.1Step 1 of 4
  1. Name

    Already ours from 0.1.0. A first release would check the name is free.

Drills

Answer fast — trust the rule.

quick drill

Which two Cargo.toml fields does crates.io require before publishing?

quick drill

You found a bug in published version 0.3.0. What do you do?

quick drill

What does cargo yank --version 0.3.0 do?

quick drill

What does cargo publish --dry-run do?

Remember

  • Add description and license, log in with a token, --dry-run, then cargo publish.
  • Check: name, metadata, version, dry-run.
  • Versions are permanent; fix forward with a new version, and yanking only hides.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainShip a fix safely

You published version 0.2.0 of a small shelf-codes crate, then found a bug. Explain how to release the fix, whether you can replace 0.2.0, and what cargo yank would and would not do.