Sitemap
Ali Aslam

Ali Aslam

Avid Learner | Principal Software Engineer | Full Stack Developer

Using External Crates in Rust (2025): Power Up Your Cargo Project🦀

4 min readAug 5, 2025

--

Part of series: Rust Tutorial for Beginners: Learn the Rust Programming Language from Scratch and Build a WebAssembly Ready App (Article Series)

Before we wrap up your Rust crash course, there’s one last superpower to unlock: Cargo and crates. Together, they streamline your development, extend your toolkit, and bring Rust’s ecosystem to your fingertips.

You can switch to video version of Rust Crash Course to the relevant chapter

Rust Tutorial — Covers Cargo, ownership, pattern matching, and memory safety in 1 hour

🛠️ What Is Cargo?

Think of Cargo as Rust’s all-in-one developer toolbelt:

  • It builds your code
  • Manages your dependencies (libraries)
  • Runs your program
  • Executes tests
  • Generates documentation

Everything you need to develop and maintain Rust projects is built in — no need to install a bunch of tools separately.

🔧 Common Cargo Commands (with Explanations)

cargo build         # Compiles your Rust project into an executable
cargo run # Builds AND runs your project in one step
cargo check # Fast type-checking without full compilation
cargo doc --open # Generates docs for your code & dependencies, then opens them in your browser

Why use cargo check? It’s much faster than a full build and great while writing code — it helps catch type errors early.

Why use cargo doc --open? It creates full HTML documentation for your code and all external crates, searchable and browsable offline.

📦 Crates: Rust’s External Libraries

Rust calls third-party libraries crates — and they are the key to extending your projects without reinventing the wheel.

🧩 What Is a Crate?

A crate is just a Rust package — it could be:

  • A utility library (like for math or JSON parsing)
  • A full framework (like a web server)
  • A tool to help with styling terminal output (like we’ll use next)

Crates are versioned and published to the official registry at crates.io.

🎨 Let’s Try One: Colored Terminal Text

Let’s walk through how to find and use a crate that styles terminal output — specifically, the colored crate.

Step 1 — Find a Crate on crates.io

Go to https://crates.io — Rust’s official crate registry.

Search for something like:

  • “colored”
  • “terminal colors”
  • “text styling”

You’ll find colored is a popular choice.

💡 Pro tip: You can also check docs.rs for detailed documentation of any crate.

Step 2 — Add It to Your Cargo.toml

Once you’ve found the crate, scroll to the Usage section. You’ll see something like this:

# In Cargo.toml
[dependencies]
colored = "2.0"

This tells Cargo to download version 2.0 of the crate and make it available to your project.

Step 3 — Use It in Your Code

Now update your main.rs file:

use colored::Colorize;
fn main() {
println!("{}", "Success!".green());
println!("{}", "Warning!".yellow());
println!("{}", "Error!".red().bold());
}

🧠 The Colorize trait adds methods like .green(), .red(), .bold() to any string. These return colored versions of the string that display in supported terminals.

This is especially useful in command-line apps for making output readable — e.g.:

  • Success messages in green
  • Errors in bold red
  • Warnings in yellow

Even though it’s simple, this crate shows how easy it is to bring external functionality into Rust.

🔍 How to Explore and Evaluate Crates

There are three main ways to explore crates:

1. crates.io

Use this to:

  • Search by keyword (e.g., “json”, “http”, “cli”)
  • Check download counts and recent updates
  • Read the crate’s README and version history

2. docs.rs

Docs.rs hosts API docs for every crate and version. It’s great for:

  • Browsing public functions and types
  • Checking example usage
  • Understanding what the crate provides

3. Search from Your Terminal

You can also search without leaving your editor:

cargo search colored

This will show you crate names and versions that match your search.

Get Ali Aslam’s stories in your inbox

Join Medium for free to get updates from this writer.

✅ Evaluate Before You Use:

  • Check if the crate is actively maintained (look for recent updates)
  • Read the README for clear setup and examples
  • See if the docs are well-written and easy to follow
  • Avoid crates that haven’t been updated in years unless necessary

📘 Best Practices for Using Crates

✅ Start with crates.io and docs.rs

Use these two together to evaluate a crate:

  • crates.io: Popularity, maintenance status, README, version
  • docs.rs: API reference, examples, trait usage, version-specific docs

🔍 Check Crate Activity & Maintenance

Ask:

  • Is it recently updated?
  • Are there active GitHub issues/pull requests?
  • Is the README clear and well-documented?

📦 Use cargo add for Convenience

Instead of editing Cargo.toml, run:

cargo add crate_name

You might need to install the cargo-edit extension.

🔄 Understand Versioning

  • Use semver-compatible versions: "2.0"
  • Avoid wildcards like "*" to prevent breaking changes

📚 Always Read the Docs

  • Visit docs.rs
  • Import the necessary traits (e.g. Colorize) to unlock methods

⚙️ Use Features Wisely

Some crates require enabling optional features:

serde = { version = "1.0", features = ["derive"] }

🧠 Don’t Over-Crate

Use crates where they help — but avoid bloating your project with too many dependencies for simple tasks.

✅ Summary

You now know how to:

  • Build, run, and document your Rust project using Cargo
  • Add external crates (libraries) using Cargo.toml
  • Find and evaluate crates using crates.io, docs.rs, or the terminal
  • Integrate real-world functionality — like colored terminal text — using community crates
  • Apply crate best practices to evaluate, manage, and use dependencies effectively

With Cargo and crates, your Rust skills go from beginner to production-ready. You’re no longer limited to what you can build from scratch — the entire Rust ecosystem is now at your fingertips.

🎯 Next time you need a library for:

  • HTTP requests
  • Parsing JSON
  • Handling dates
  • Styling terminal output
  • Building full web apps

…check crates.io first.

You’re now ready to build like a real Rustacean 🚀

🎯 “Learning to navigate crates.io is one of the most empowering steps in your Rust journey. You’ll go from writing code to composing powerful applications — fast.”

--

--

Ali Aslam
Ali Aslam

Written by Ali Aslam

Avid Learner | Principal Software Engineer | Full Stack Developer