Skip to Content
Course content

126: Publishing a Crate to crates.io

Click on the "Edit" button in the top corner of the screen to edit your slide content.

One of the most stressful moments for a new Rustacean is the first time they run cargo publish. I see a lot of people treat this like a git push. They assume that if they spot a typo in their documentation or a glaring bug in their logic five minutes after publishing, they can just fix the code, run the command again, and "update" the version they just sent out.

The "I'll just overwrite the version" Myth

Here is the cold, hard truth: crates.io is immutable. Once you publish version 0.1.0 of your crate—let's call it json-shorthand—that specific version is etched in stone. If you realize that your trim_keys function panics when it encounters an empty string, you cannot simply push a fix to 0.1.0. If you try, Cargo will scream at you, telling you that the version already exists.

I've been there. Early in my career, I published a crate with a debug println! that leaked a local file path. I spent ten minutes frantically trying to "delete" the version, only to realize that while you can yank a version (telling Cargo not to use it for new builds), the code is still physically there for anyone who has a direct link to it. This realization changes how you approach the publishing process; it turns it from a "push" into a "release event."

Preparing your Cargo.toml for the Public

Before you even think about the API key, your Cargo.toml needs to be more than just a list of dependencies. When you're developing locally, you can get away with a blank description. On crates.io, that makes your crate invisible to anyone searching for a tool. I always suggest filling these out in this specific order:

  • Description: Be precise. Don't say "A JSON tool." Say "A utility for shortening JSON keys to save bandwidth in API responses."
  • License: If you don't specify this, your crate is essentially unusable for professional projects. Use MIT OR Apache-2.0—it's the industry standard for Rust.
  • Repository: Link your GitHub or GitLab. Users will want to file issues when your code eventually breaks their build.
  • Keywords: Add a few tags. For our example, json, serialization, and optimization.
[package]
name = "json-shorthand"
version = "0.1.0"
edition = "2021"
description = "A utility for shortening JSON keys to save bandwidth in API responses"
license = "MIT OR Apache-2.0"
repository = "https://github.com/yourname/json-shorthand"
keywords = ["json", "serialization", "optimization"]

The Handshake with crates.io

Now for the actual plumbing. You can't just push code; you need an identity. You'll head over to crates.io, create an account, and generate an API token. I highly recommend using a "Publish" token rather than a full-access account token if you're setting up a CI/CD pipeline later.

Once you have the token, you run cargo login and paste it in. From that point forward, Cargo stores that token locally. When you finally run cargo publish, Cargo does something very important: it packages your source code, checks that your Cargo.lock isn't being uploaded (since libraries shouldn't include lockfiles), and verifies that your documentation is valid.

One pro tip: Always run cargo test and cargo doc --open one last time before publishing. There is nothing more embarrassing than publishing a crate where the main example in the README doesn't actually compile.




📋 Practical Task

Publishing the reading-time-calc Utility Crate

You have been tasked with releasing a small utility crate called reading-time-calc. This crate provides a single function that takes a string of text and returns the estimated reading time in minutes based on an average of 200 words per minute.

Your goal: Prepare the crate for public consumption and simulate the publishing workflow.

  1. Metadata Audit: Open your Cargo.toml. Ensure you have added a professional description, a dual license (MIT/Apache-2.0), a placeholder repository URL, and at least three relevant keywords.
  2. The Dry Run: Instead of publishing for real (which would require a unique name not already taken on crates.io), run cargo publish --dry-run.
  3. Verification: Analyze the output of the dry run. Check if Cargo warns you about missing metadata or problematic files being included in the package.
  4. Documentation Check: Run cargo doc --no-deps and ensure that your public functions have /// doc comments with an # Examples section that would actually compile if tested.

Note: Since you cannot use a name that is already taken on the live registry, the --dry-run flag is your best friend here to verify that your package is "publish-ready" without actually attempting to claim the name on the server.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.