Course contentsShow
← Rust
Lesson 18 of 1,9101. Rust Fundamentals and ToolingFree lesson

Publishing Crates to Crates.io

Package your library and share it with the community while maintaining security and versioning standards.

In this lesson, you will learn how to package your Rust library and share it with the world by publishing it to crates.io, the official package registry for the Rust community.

Preparing Your Crate for Distribution

Before others can use your code, you must ensure your project is properly identified. All of this metadata lives in your Cargo.toml file. You must include at least a name, a version, a description, a license, and a repository URL.

Here is what that looks like in your Cargo.toml:

[package]
name = "my-awesome-library"
version = "0.1.0"
edition = "2021"
description = "A helpful utility for string manipulation."
license = "MIT OR Apache-2.0"
repository = "https://github.com/username/my-awesome-library"

Security and Versioning

Sharing code requires responsibility. When you publish a crate, you are essentially promising the community that this version will remain available. Because of this, crates can never be deleted from crates.io. You can "yank" a version to prevent new projects from downloading it, but existing projects relying on that version will not break.

Versioning follows Semantic Versioning (SemVer). If you make breaking changes to your library, you must increment the major version (e.g., from 0.1.0 to 0.2.0). This ensures that your users' projects don’t suddenly stop compiling when they run cargo update.

The Publishing Process

To get started, you will need a crates.io account. Once you are logged in, you will get an API token. Run this command in your terminal to authenticate your local machine:

cargo login <your-api-token>

Once authenticated, you simply run the following command from your project root:

cargo publish

Before the upload, Cargo will perform a "dry run" to verify your files are packaged correctly and your Cargo.toml contains all the mandatory fields. If everything looks good, your code is uploaded, indexed, and becomes instantly available for anyone in the world to add as a dependency to their own projects.

Key Takeaway: Publishing to crates.io turns your local code into a global dependency, requiring clear metadata and a commitment to stable, semantic versioning.