🎓 A1Learner
🔍

Rust Get Started

Cargo is the Rust tool that creates, builds, and runs projects. Cargo creates the folder, the settings file, and a first program.

Create a project

Cargo creates the folder, the settings file and a first program for you:

# in your terminal:
# $ cargo new my_app

Build and run in one step

This is the command you will type most. It builds the project, then runs it:

# in your terminal:
# $ cargo run

Build without running

cargo build only builds. The program lands in target/debug. Add --release for a fast, final build — that one lands in target/release:

# in your terminal:
# $ cargo build
# $ cargo build --release

Check quickly

cargo check reads your code and reports mistakes, without a full build — the fastest way to ask "is this right?":

# in your terminal:
# $ cargo check

The settings file

Cargo.toml lists your project settings and extra libraries. It is written in TOML, a simple format made for settings files:

[package]
name = "my_app"
version = "0.1.0"

[dependencies]

Two more ways to start

Turn an existing folder into a Cargo project. Or start a library. A library is a project with src/lib.rs and no main function. Other projects can use it:

# in your terminal:
# $ cargo init
# $ cargo new name --lib

Example

fn main() {
    println!("Hello, world!");
    println!("Cargo built and ran this.");
}

Key points

  • Cargo.lock: a file Cargo writes for you.
  • It lists the exact versions of your libraries.
  • So the same build can be repeated later
  • Most lessons in this course use Cargo.

In this example, two commands come first. cargo new creates the hello project. cargo run starts it. The code lives in src/main.rs. The first println! prints "Hello, world!". The second println! prints the next line. Each println! ends the line, so the two messages appear one below the other.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

Which command builds and runs a Cargo project in one step?