Working with Cargo

In the previous chapter we saw how we can manually compile programs using the Rust compiler. While this works, it isn't very convenient especially if we decide to split our code in multiple files or modules.

To make development easier, Rust uses a package manager called cargo. Cargo comes already installed with Rust, so there's nothing additional we need to set up or configure to get started.

What does cargo do?

Cargo is a tool that automates things like compiling code, managing dependencies, and generating documentation. Cargo simplifies the process of building and sharing Rust projects by providing a consistent way to manage packages, versioning, and dependencies. You can also use it to run tests. In essence, once you start using cargo, you won't need any other tools to do most of the work like you would need in Python.

You can also use cargo to generate & scaffold new Rust projects (binaries or libraries) which we will do right now.

Getting started with cargo

Let's start by creating a new Rust project using cargo. Open a terminal window on your Desktop or any other directory you'd like to write the code in and run the following command:

cargo new data-with-rust

# Output:
#   Created binary (application) `data-with-rust` package

This creates a directory called data-with-rust with a bunch of files for a default hello world project (additionally to some files for .git, which I'm ignoring for now)

# files in the data-with-rust folder

data-with-rust/Cargo.toml  # dependency file, similar to requirements.txt in Python
data-with-rust/src/main.rs # contains Rust code

Cargo build

The Rust code that we'll be writing goes mainly into the data-with-rust/src/main.rs file, but before we get into that, let's use cargo a bit more and start with building our project.

cd data-with-rust/

cargo build 

# Output
#   Compiling data-with-rust v0.1.0 (/Users/karim/tests/data-with-rust)
#    Finished dev [unoptimized + debuginfo] target(s) in 0.51s

If you look carefully at the output and the directory, you'll see a new folder called target/ has been created, along with a bunch of other files. This folder contains the compiled & built executable as well as other things we'll elaborate on much later in this course.

You can run the code the binary that we've built directly:

./target/debug/data-with-rust

# Output
# Hello, world!

This might look very similar to the step we've done previously using rustc. In fact, behind the scenes, cargo is using rustc.

Cargo run

We can also do the above two steps in one single step:

cargo run 

# Output
#    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
#     Running `target/debug/data-with-rust`
# Hello, world!

Executing cargo run bundles together many steps in one. In a nutshell, it's building the code and then running it saving us precious seconds of our valuable time already. Right now, it isn't building the code since there were no changes made to the code, but let's do just that

// filename: src/main.rs
fn main() {
    println!("Hello, world! Let's use data with Rust!");
}

And run cargo run again. You'll notice that the output is now different.

Recap

Now this only scratches the surface of what you can do using cargo, but this will already get us extremely far. Next, we'll have a quick look at some basic Rust syntax.