Rust
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Ownership and Borrowing
-
Section 4: Structuring Data
-
Section 5: Collections and Error Handling
-
Section 6: Traits and Generics
-
Section 7: Concurrency
-
Section 8: Building for the Web
-
Section 9: Memory and Performance
-
Section 10: More Standard Library and Ecosystem
-
Section 11: Advanced Rust
-
Section 12: Rust for Systems and WebAssembly
-
Section 13: Tooling and Best Practices
-
Section 14: Data Structures and Algorithms in Rust
-
Section 15: Practical Projects
-
Section 16: Interview Practice
-
Section 17: std::collections In Depth
-
Section 18: std::io and std::fs In Depth
-
Section 19: std::net
-
Section 20: std::option and std::result In Depth
-
Section 21: std::iter In Depth
-
Section 22: std::sync In Depth
-
Section 23: std::string and std::str
-
Section 24: Cargo and Crates.io Ecosystem
-
Section 25: Popular Crates Ecosystem
-
Section 26: Rust Memory Model Deep Dive
-
Section 27: More Practice Exercises
-
Section 28: More Interview Practice
-
Section 29: Async Rust Deep Dive
-
Section 30: Tokio Ecosystem In Depth
-
Section 31: Error Handling Ecosystem Deep Dive
-
Section 32: Serde In Depth
-
Section 33: Web Development with Rust Deep Dive
-
Section 34: Database Access Ecosystem
-
Section 35: Rust for Embedded Systems Deep Dive
-
Section 36: Rust Macros In Depth
-
Section 37: Advanced Trait System
-
Section 38: Unsafe Rust In Depth
-
Section 39: Rust CLI Development
-
Section 40: Testing Ecosystem Deep Dive
-
Section 41: WebAssembly Deep Dive
-
Section 42: Rust Design Patterns
-
Section 43: More Data Structures in Rust
-
Section 44: Final Practice Projects
-
Section 45: Rust Performance Optimization
-
Section 46: Rust Ecosystem Tooling
-
Section 47: More Interview and Review
126: Publishing a Crate to crates.io
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, andoptimization.
[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.
- Metadata Audit: Open your
Cargo.toml. Ensure you have added a professionaldescription, a duallicense(MIT/Apache-2.0), a placeholderrepositoryURL, and at least three relevantkeywords. - 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. - Verification: Analyze the output of the dry run. Check if Cargo warns you about missing metadata or problematic files being included in the package.
- Documentation Check: Run
cargo doc --no-depsand ensure that your public functions have///doc comments with an# Examplessection 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.
There are no comments for now.