Skip to Content
Course content

3: Cargo Projects and Dependencies

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Think of starting a Rust project without Cargo like trying to build a custom dining table by forging your own nails and sawing your own planks from a fallen tree. Sure, you could do it, but why would you? In the real world, you'd go to a hardware store, buy pre-cut lumber, and use a set of standardized tools. You'd have a blueprint that lists exactly what materials you need so that if someone else wanted to build the same table, they could just follow your list.

In Rust, Cargo is that hardware store, the blueprint, and the assembly line all rolled into one. Your Cargo.toml file is the blueprint—it tells Rust exactly what "materials" (dependencies) your project needs to function. The src folder is your workbench where the actual building happens, and the cargo command is the power tool that puts it all together.

Setting up your workspace

Until now, you've probably been running rustc on single files. It's fine for snippets, but it falls apart the moment you have multiple files or need a library. To start a real project, I always use cargo new.

cargo new password_gen
cd password_gen

When you do this, Cargo doesn't just make a folder; it scaffolds a whole environment. You'll see a Cargo.toml file and a src/main.rs. I want you to pay close attention to that .toml file. It's written in Tom's Obvious, Minimal Language, and it's where you'll spend a lot of your time managing how your project is configured.

Borrowing code from the community

The real magic of Cargo is crates.io. A "crate" is just a Rust package. Instead of writing a random number generator from scratch—which is actually surprisingly hard to do correctly—you can just borrow a crate that someone else has already perfected.

Let's say we want to build a simple password generator. We need a way to pick random characters. I'd use the rand crate for this. To add it, you don't download a zip file or move folders around; you just add one line to your Cargo.toml under the [dependencies] section:

[dependencies]
rand = "0.8.5"

The moment you run cargo build or cargo run, Cargo looks at that line, reaches out to the internet, downloads the exact version of rand you asked for, and links it to your project. It also creates a Cargo.lock file. Don't touch that file manually—it's Cargo's way of remembering the exact version of every single sub-dependency used, ensuring that if you send your code to a teammate, it builds exactly the same way on their machine as it does on yours.

Putting it all together

Now that we've told Cargo we need rand, we can actually use it in our code. I'll keep this simple: we'll define a string of possible characters and use the crate to pick from them.

use rand::Rng; // Import the trait that gives us the random functionality

fn main() {
    let charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*";
    let mut rng = rand::thread_rng();
    let mut password = String::new();

    for _ in 0..12 {
        let idx = rng.gen_range(0..charset.len());
        password.push(charset.as_bytes()[idx] as char);
    }

    println!("Your new secure password is: {}", password);
}

To run this, you just type cargo run. This one command handles everything: it compiles your dependencies, compiles your code, and executes the resulting binary. It's a massive quality-of-life improvement over calling the compiler manually every time you make a typo.




📋 Practical Task

Build a Randomized Dice-Roll Simulator

Your goal is to create a new Cargo project that simulates rolling two six-sided dice. This will test your ability to initialize a project, manage a dependency, and implement the logic in the src folder.

Requirements:

  • Create a new project named dice_roller using Cargo.
  • Add the rand crate to your Cargo.toml.
  • In main.rs, generate two random integers between 1 and 6 (inclusive).
  • Print the result of each die and the total sum of the roll to the console.
  • Run the program using cargo run and verify that the output changes each time you execute it.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.