DEV Community

Subesh Yadav
Subesh Yadav

Posted on

Day 25 of #100DaysOfRust: Customizing Builds and Publishing to Crates.io

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 the dev profile (unoptimized + debug info)
  • cargo build --release uses the release 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
Enter fullscreen mode Exit fullscreen mode

Default Optimization Levels

# Cargo.toml
[profile.dev]
opt-level = 0

[profile.release]
opt-level = 3
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

Run:

cargo doc --open
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Crate-Level Documentation

Use //! for crate-level or module-level descriptions:

//! # My Crate
//!
//! `my_crate` is a collection of utilities to simplify calculations.
Enter fullscreen mode Exit fullscreen mode

📤 Setting Up Crates.io

1. Create an Account

2. Get API Token

  • Visit crates.io/me and copy your API key.
  • Authenticate locally:
cargo login
Enter fullscreen mode Exit fullscreen mode

🧾 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"
Enter fullscreen mode Exit fullscreen mode

🚀 Publishing the Crate

cargo publish
Enter fullscreen mode Exit fullscreen mode

⚠️ Note: Publishing is permanent. You cannot delete a published version.


♻️ Publishing New Versions

Update the version in Cargo.toml:

version = "0.2.0"
Enter fullscreen mode Exit fullscreen mode

Then publish:

cargo publish
Enter fullscreen mode Exit fullscreen mode

Follow Semantic Versioning rules.


⛔ Yank Broken Versions

Prevent new projects from using a version:

cargo yank --vers 1.0.1
Enter fullscreen mode Exit fullscreen mode

Undo a yank:

cargo yank --vers 1.0.1 --undo
Enter fullscreen mode Exit fullscreen mode

🌍 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 {
        // ...
    }
}
Enter fullscreen mode Exit fullscreen mode

Make it easier for consumers to import:

use art::PrimaryColor;
use art::mix;
Enter fullscreen mode Exit fullscreen mode

🧠 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)