Getting Started with Rust and Cargo: A Modern Package Manager for Systems Programming

Getting Started with Rust and Cargo: A Modern Package Manager for Systems Programming

Rust has become one of the most talked-about programming languages in recent years. Born at Mozilla, it was designed with three goals in mind: memory safety, fearless concurrency, and blazing performance. Unlike traditional systems languages like C or C++, Rust guarantees safety without a garbage collector, relying instead on its innovative ownership system.

But there’s another reason why developers love Rust: Cargo, Rust’s built-in package manager and build tool. If you’ve ever used pip in Python or npm in JavaScript, Cargo will feel familiar — but with some extra superpowers.


Why Rust Stands Out

Rust’s appeal is more than just hype. It offers:

  • Zero-cost abstractions: High-level code compiles down to efficient machine code.
  • Ownership and borrowing: Memory is managed at compile time, eliminating many runtime errors.
  • Concurrency without fear: The compiler ensures safe multithreaded code.

These features make Rust popular in everything from embedded systems to web backends to WebAssembly modules.


Meet Cargo: Rust’s All-in-One Tool

Cargo handles everything from creating new projects to managing dependencies and building binaries. Think of it as the Swiss Army knife of Rust development.

Creating a New Project

cargo new hello_rust
cd hello_rust
cargo run

This creates a Cargo.toml file (like requirements.txt in Python) and a starter main.rs.

Adding Dependencies

To use an external crate (Rust’s term for packages):

# In Cargo.toml
[dependencies]
serde = "1.0"
reqwest = "0.11"

Then build:

cargo build

Cargo will fetch, compile, and lock versions automatically — no manual juggling.

Running Tests

Rust has testing built in:

cargo test

You’ll see a full test suite runner without extra setup.


Cargo vs Pip: Why It Matters

If you come from Python, pip is just a package installer. Cargo goes further:

  • Build system: Compiles your code with cargo build.
  • Dependency manager: Handles external crates and version locking.
  • Testing: Runs tests with cargo test.
  • Documentation: Generates docs with cargo doc.
  • Publishing: Uploads your crate to crates.io with cargo publish.

In short, Cargo isn’t just pip — it’s pip, pytest, setuptools, and Sphinx rolled into one.


Rust and WebAssembly

Rust’s performance and memory safety make it a great fit for WebAssembly (Wasm), which allows you to run near-native code in the browser.

With Cargo, targeting Wasm is straightforward:

rustup target add wasm32-unknown-unknown
cargo build --target wasm32-unknown-unknown

Your Rust code is now compiled into .wasm, ready to be used in a web app. This opens doors for fast, safe client-side applications written in Rust but running in any browser.


Example: Building a Small CLI Tool

Let’s create a command-line program that prints a random greeting.

Run it:

cargo run

Edit main.rs:

use rand::seq::SliceRandom;

fn main() {
    let greetings = ["Hello", "Hola", "Bonjour", "Konnichiwa"];
    let choice = greetings.choose(&mut rand::thread_rng()).unwrap();
    println!("{} from Rust!", choice);
}

Add a dependency (rand crate):

[dependencies]
rand = "0.8"

Create a project:

cargo new greeter
cd greeter

Congratulations — you’ve built and run a Rust project with external dependencies using Cargo.


Final Thoughts

Rust is often praised for its compiler and ownership model, but Cargo is the unsung hero. It removes friction, streamlines workflows, and makes Rust approachable even for newcomers. And when paired with modern targets like WebAssembly, it bridges the gap between low-level performance and modern development needs.

So, if you’re coming from Python’s pip, Node’s npm, or Java’s Maven — think of Cargo as the all-in-one, batteries-included toolkit that makes Rust not just powerful, but delightful to use.

To Learn more about Rust, please check out this book.

Read more