Welcome to Day 25 of my Rust journey! Today, I explored how to customize builds with release profiles and how to publish crates to crates.io, Rust’s official package registry. These topics are crucial when preparing a production-ready crate or sharing your project with the community.
🔧 Customizing Builds with Release Profiles
Rust provides predefined and customizable build profiles via Cargo, offering control over compile-time options.
Dev vs. Release Profiles
-
cargo build
uses thedev
profile (unoptimized + debug info) -
cargo build --release
uses therelease
profile (optimized)
$ cargo build
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
$ cargo build --release
Finished `release` profile [optimized] target(s) in 0.32s
Default Optimization Levels
# Cargo.toml
[profile.dev]
opt-level = 0
[profile.release]
opt-level = 3
-
opt-level
ranges from 0 to 3. - Use lower levels for faster dev builds.
- Use level 3 for optimized release builds.
Overriding Defaults
Customize profiles by overriding defaults in Cargo.toml
:
[profile.dev]
opt-level = 1
Use Cargo’s documentation for full customization options.
📦 Publishing a Crate to Crates.io
Once your crate is ready, you can publish it for others to use via crates.io.
1️⃣ Document Your Code
Use documentation comments (///
) with Markdown:
/// Adds one to the number given.
///
/// # Examples
///
/// let arg = 5;
/// let answer = my_crate::add_one(arg);
/// assert_eq!(6, answer);
///
pub fn add_one(x: i32) -> i32 {
x + 1
}
Run:
cargo doc --open
To generate and view documentation.
Common Doc Sections
# Examples
# Panics
# Errors
# Safety
Doc Comments as Tests
Examples in ///
comments are tested when running:
cargo test
Crate-Level Documentation
Use //!
for crate-level or module-level descriptions:
//! # My Crate
//!
//! `my_crate` is a collection of utilities to simplify calculations.
📤 Setting Up Crates.io
1. Create an Account
- Sign in using GitHub at crates.io.
2. Get API Token
- Visit crates.io/me and copy your API key.
- Authenticate locally:
cargo login
🧾 Add Metadata to Cargo.toml
Required fields:
[package]
name = "guessing_game"
version = "0.1.0"
edition = "2024"
description = "A fun game where you guess the number."
license = "MIT OR Apache-2.0"
- SPDX License List for license identifiers.
🚀 Publishing the Crate
cargo publish
⚠️ Note: Publishing is permanent. You cannot delete a published version.
♻️ Publishing New Versions
Update the version in Cargo.toml
:
version = "0.2.0"
Then publish:
cargo publish
Follow Semantic Versioning rules.
⛔ Yank Broken Versions
Prevent new projects from using a version:
cargo yank --vers 1.0.1
Undo a yank:
cargo yank --vers 1.0.1 --undo
🌍 Exporting Public API with pub use
Structure your public API without exposing internal module hierarchy:
// src/lib.rs
pub use self::kinds::PrimaryColor;
pub use self::utils::mix;
pub mod kinds {
pub enum PrimaryColor {
Red,
Yellow,
Blue,
}
}
pub mod utils {
use crate::kinds::*;
pub fn mix(c1: PrimaryColor, c2: PrimaryColor) -> SecondaryColor {
// ...
}
}
Make it easier for consumers to import:
use art::PrimaryColor;
use art::mix;
🧠 Summary
- Custom release profiles speed up dev and optimize releases.
- Documentation is critical for crate usability.
- Publishing requires proper metadata and account setup.
- Crates.io is permanent—use
yank
to deprecate versions. - Use
pub use
to shape your public API.
Tomorrow, I’ll explore workspaces and managing multi-crate projects in Rust!
🚀 Follow me for more #100DaysOfRust progress and tips!
Top comments (0)