← Retour au cours: Rust from scratch

Installing Rust and your first cargo project

Vidéo bientôt disponible

Rust is a compiled language designed for writing fast and safe programs: the compiler catches memory errors at compile time that in other languages only show up when the program is already in production. This course assumes you have programmed before in some other language (Python, JavaScript, Java, C...), so we will go straight to what makes Rust special. We start with the practical part: installing the tools and running a program.

Installing with rustup

The official way to install Rust is rustup, a manager that downloads the compiler (rustc) and the project manager (cargo), and lets you update or switch versions. On Windows, download the installer from rust-lang.org/tools/install; on Linux and macOS run the command shown on that same page. On Windows you also need the Visual Studio C++ build tools (the installer tells you). When it finishes, open a new terminal and check the installation with the commands below. To update later, just run rustup update.

rust
# check that everything is installed
rustc --version
cargo --version
rustup update

If the terminal answers with version numbers, you are ready. If it says the command does not exist, it is almost always because the terminal was opened before installing: close it and open a new one so it reads the updated PATH.

A hand-made program with rustc

Before using cargo, let us see the bare minimum. A Rust program starts in the main function. The println! line ends with an exclamation mark because it is a macro, not a normal function; for now just remember that it prints text and adds a newline. Save this as hello.rs and compile it with rustc hello.rs; it produces an executable you run directly.

rust
fn main() {
    // println! is a macro: it prints text plus a newline
    println!("Hello, Rust!");
}

Cargo: the center of the workflow

In real projects you almost never call rustc by hand. Cargo creates the structure, builds, runs, downloads dependencies and runs tests. cargo new name creates a folder with a Cargo.toml file (name, version, edition and dependencies) and src/main.rs with a Hello world already written. The 2024 edition is the newest one and cargo new sets it by default.

rust
cargo new hello_cargo
cd hello_cargo
cargo run      # compile + run (debug mode)
cargo check    # only checks errors, much faster
cargo build --release   # optimized binary in target/release
  • cargo run: builds (if needed) and runs.
  • cargo check: verifies the code compiles without producing the executable; use it while you write.
  • cargo build: produces the binary in target/debug.
  • cargo build --release: produces the optimized binary in target/release (slower to build, faster to run).

Common mistake: forgetting that target/ is a generated folder. You do not edit it or commit it to git (cargo new already creates a .gitignore that excludes it). Another frequent mistake is measuring speed with cargo run: debug mode is slow on purpose; to measure, use --release.

Reading the compiler messages

Rust error messages are famous for being clear: they show the file, the line, what was expected and often how to fix it. Get used to reading them from the top and fixing the first error first; the next ones are often a consequence of it. Write your program with a deliberate typo (for example printn! instead of println!) to see how the compiler answers.

rust
fn main() {
    let name = "Ada";
    // {} inserts a value; {name} uses the variable directly
    println!("Hello, {}!", name);
    println!("Hello again, {name}!");
}

Testez vos acquis

Question 1 sur 1

Which command checks for compile errors without producing the executable?

✎ Mes notes

Enregistré seulement dans ce navigateur. Connectez-vous pour le garder dans votre compte.0/5000 caractères

Suivante →