Implementing the First Rust Project
Get started with Rust by working on your first project.
We'll cover the following
Throughout this course, we’ll use Cargo for our Rust projects and install and manage packages for the projects. It is essential for us to get familiar with the Rust package manager before we can dive into the semantics of the language itself.
What is Cargo?
Cargo is a package manager and build tool for Rust. It serves as a central tool for managing Rust projects by handling tasks such as dependency management, testing, and packages. Cargo streamlines the process of creating, building, testing, and distributing Rust applications and libraries. We’ll learn more about how we can accomplish each task as we go along in the course. For now, let’s see how we can get started with creating a Rust project using Cargo.
The command we can use to create a new Rust project is as follows:
cargo new <project> # Replace <project> with the name of your project.
This command will create a folder with the name specified by <project>
and will have the following hierarchy:
<project>├── src├── main.rs├── Cargo.toml
We can see that the project that Cargo just created contains multiple files:
The
src/
main.rs
file: We can use the Rust compiler to compile our code in themain.rs
file. This creates an executable that we can execute to run our code. Themain.rs
file is our program’s entry point. This is how the program will know where to start.The
Cargo.toml
file: This file is used by Cargo to specify project information, and it’s present in the root directory of the project. It also contains a list of dependencies that the project uses and their version numbers.
Note: We can use
cargo add <dependency>
to add a new dependency to our project. Running this command will also add the dependency, along with its version number to theCargo.toml
file. We can also add a dependency to the project by adding it in theCargol.toml
file and running the project will automatically install it.
Here, a terminal that runs this command and lists all the files created after running the command.
Running the project
Now that we’ve had a brief look at what Cargo is and how it works, let’s take a look at how we can run the project. Our main.rs
file, by default, contains the following code:
fn main() {println!("Hello, world!");}
The Rust compiler runs the code in the main()
function, and we can execute the main function using Cargo, by running the following commands:
cargo build# and thencargo run
cargo build
will create a folder calledtarget
which will contain the executable files for the project.cargo run
will run the executable file.
Note: We can directly use
cargo run
to run the project without usingcargo build
because Cargo does that for us automatically. Another interesting point to note is that these commands can be run from anywhere inside the project directory, as long as there is aCargo.toml
in the project directory.
Building the project
Let’s talk a little about building projects. There are two ways we can build a project:
Debug build
-
By default, Cargo will build the project in the debug mode, i.e., when we use the
cargo build
command. -
This mode offers minimal optimization for the application to make debugging easier.
-
The time it takes to build the application is typically faster than a release build making it convenient for developers when frequent code changes are recompilations required.
Release build
-
We can specify a release build for our project by passing the
--release
flag to ourcargo build
command. -
Release builds enable aggressive compiler optimizations to generate highly efficient machine code resulting in optimized and faster executables.
-
Release builds might take longer to compile due to the increased level of optimization.
Note: If we want to run the release build instead of the debug build, we can directly execute the release executable in the
./target/release/<project>
directory. Replace<project>
with the name of your project.
Some cargo
commands
Here’s a list of useful commands that we can use with cargo
. We’ll use most of them for building our project.
Command | Purpose |
| Creates a new project |
| Builds the project |
| Runs the project |
| Runs a test defined in the project |
| Creates documentation for the project code |
| Formats all code inside the project |
| Updates the dependencies inside the |
You can use the terminal below to test out these commands inside a new project that you’ll create using the cargo new
command.