Skip to content
Theory · Your First Program · lesson 3 of 7

A project with Cargo

In the previous lesson we wrote our first programs but did not run them yet. Now let's see what builds and runs them and what Cargo does.

If a program is a recipe, a project is a folder where the recipe lies together with everything that comes with it. It holds a card with the name and the version, and after a build a shelf with finished dishes appears. Cargo manages this folder: it creates it, hands the recipe to the compiler-editor, and sends the approved recipe to the kitchen.

Why a folder, if the program fits in one file? A real program grows fast: it gets new files, other people's libraries, and different ways to build it. Cargo gives every project a standard shape, so all Rust projects are laid out the same way.

Here the picture stops working: Cargo compiles nothing itself. The compiler, rustc, still checks the text and translates it into machine code, and Cargo only calls it with the right settings.

How to run a program

Rust comes with three tools:

  • rustup installs and updates the other tools;
  • rustc is the compiler itself;
  • cargo manages a project: it calls the compiler and runs what it built.

People rarely call rustc by hand. The program goes into a Cargo project, in the file src/main.rs, and the cargo run command builds it and runs it right away. You type commands in the terminal, a window for text commands. If the compiler finds an error, the program does not run, and its message appears in the terminal.

The cargo new command

The command cargo new hello creates a folder hello with a ready project:

TEXT
hello/
├── .gitignore
├── Cargo.toml
└── src/
    └── main.rs

The folder holds three files:

  • Cargo.toml is the project's description;
  • src/main.rs is the program's code: Cargo puts an example there right away that prints Hello, world!;
  • .gitignore is a service file for the git version control system, which you can leave alone for now.

The Cargo.toml file

Here is what Cargo writes into the description of a new project:

TOML
[package]
name = "hello"
version = "0.1.0"
edition = "2024"

[dependencies]

Let's go through it line by line:

  1. [package] opens the section that describes the project.
  2. name is the project's name, and the finished program gets the same name.
  3. version is the project's version number, 0.1.0 for a new project.
  4. edition is the language edition.
  5. [dependencies] is the section for other people's libraries, empty for now.

An edition is a set of language rules. Editions come out every three years: 2015, 2018, 2021 and 2024. A new edition may change the rules so that old code would stop building, so every project picks its own.

Interviewers often ask how an edition differs from a compiler version. The version is the number of rustc itself, such as 1.99, and a new one comes out every six weeks. The edition is chosen in Cargo.toml, and rustc 1.99 builds code of any edition.

Three Cargo commands

Cargo commands are run in the project folder. They treat the result differently, so let's take each one separately.

cargo build: build

cargo build calls the compiler and puts the finished program in the folder target/debug: the file hello, or hello.exe on Windows. The folder target is that very shelf with finished dishes. You run the program from there separately and as many times as you like, without a new build.

cargo run: build and run

cargo run builds the program the same way as cargo build and runs it right away. Here is what appears in the terminal:

TEXT
   Compiling hello v0.1.0 (/home/anna/hello)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.20s
     Running `target/debug/hello`
Hello, world!

Let's go through the output line by line:

  1. Compiling: Cargo called the compiler for the project hello.
  2. Finished: the build is done, and dev means a debug build, covered below.
  3. Running: Cargo starts the program from target/debug.
  4. Hello, world! is the output of the program itself.

If the code has not changed since the last build, Cargo does not compile it again, and the next run shows no Compiling line.

cargo check: check only

cargo check runs all the compiler's checks and reports the same errors and warnings as cargo build. But it stops before the translation into machine code, so no program appears and there is nothing to run. In return, the check is much faster than a build, and people run it all the time while writing code.

cargo check does not check how the code is laid out, such as indentation and spaces. A separate command, cargo fmt, does that: it fixes the indentation by the common rules on its own.

Debug and release builds

By default Cargo makes a debug build, shown as the dev profile in the output. It compiles fast, but the compiler spends no time on optimization, that is, on making the program faster. In return, the program keeps extra checks that help find errors.

The command cargo build --release makes a release build and puts the program in target/release. Compiling takes longer, and the program runs many times faster. That is why a program's speed is measured only in a release build, and interviewers like to ask about that too.

Common mistake

A Cargo command is run outside the project folder, for example in the home folder:

TEXT
$ cargo run
error: could not find `Cargo.toml` in `/home/anna` or any parent directory

The $ sign is the terminal prompt; you do not type it. Cargo looks for the file Cargo.toml in the current folder and in the folders above it. Without it, Cargo does not know which project to build. Open the terminal in the project folder or go into it with the command cd hello, and run cargo run again.

Key points

  • cargo new creates a project: the description in Cargo.toml, the code in src/main.rs.
  • The edition in Cargo.toml is a set of language rules, not the compiler version.
  • cargo build builds the program into target/debug, and cargo run builds it and runs it right away.
  • cargo check checks the code but builds no program: the errors are the same, and there is nothing to run.