BeginnerLesson 1 of 155 min read

Your First Rust Program: rustc, cargo and Immutable by Default

Write and run a first Rust program, learn what rustc and cargo do, see that variables are immutable by default, and compare the real debug and release builds of the same program.

In this lesson you will

  • Write and run a Rust program
  • Explain immutability and mut
  • Know what rustc and cargo do
  • Compare debug and release builds

About 28 minutes

Ines wants to watch her wind turbines

Ines maintains a small wind farm and wants a program that reads turbine measurements, flags problems and keeps working for years without crashing at three in the morning. She has chosen Rust, a language that compiles to fast machine code like C and C++, but checks at compile time that the program cannot use memory after freeing it, cannot have two parts changing the same data at once, and cannot dereference a null pointer. Those checks are the reason people learn Rust, and the price is that the compiler is strict and talkative. This course treats its messages as a teacher.

A Rust program starts in the function main. The macro println! prints a line, and an exclamation mark after a name tells you that it is a macro, which can take a format string with {} placeholders. The compiler is rustc, and the everyday tool is cargo, which creates projects, builds them, runs tests and downloads libraries, called crates. In Rust, a variable is declared with let, and it is immutable by default: once it has a value, it cannot change unless you write let mut. This default sounds restrictive and is a good one, because a value that cannot change cannot surprise you. Every value has a type, usually inferred by the compiler.

Command What it does
cargo new name Create a project
cargo run Build and run
cargo build –release Optimised build
cargo test Run the tests
cargo fmt, cargo clippy Format code, and lint it
fn main() {
    let turbines = 14;
    let wind_speed = 11.5;
    let mut alarms = 0;
    alarms += 2;
    println!("Ines's wind farm");
    println!("Turbines: {}, wind speed: {} m/s, alarms: {}", turbines, wind_speed, alarms);
    let name = "North field";
    println!("{} has {} turbines", name, turbines);
}
Ines's wind farm
Turbines: 14, wind speed: 11.5 m/s, alarms: 2
North field has 14 turbines

The program printed the turbine count, the wind speed and the alarms, which went from 0 to 2 because the variable was declared with mut. Without mut, the line alarms += 2 would be a compile error. The compiler in this course is “rustc 1.97.0 (2d8144b78 2026-07-07)”.

To see what the tools produce, the course built the same source twice. The debug build, the default, takes 4,387,344 bytes and the optimised build, made with -O, takes 4,385,040 bytes, and both start with the four bytes 7F, E, L and F, the Linux executable signature. A tiny program is large in Rust because the standard library’s formatting and panic machinery is linked in, and the debug build also carries debug information. The two builds differ in speed far more than in size: optimised code can be tens of times faster for heavy computation, which is why you measure performance only with cargo build –release, never with the debug build.

Over the next lessons Ines’s small program will grow into a real monitor: it will read measurements, classify them, handle bad data without crashing, run on several threads and carry its own tests. Each step uses only the standard library, so everything you see can be reproduced on any machine with a Rust toolchain installed, and nothing depends on downloading outside code.

Things that trip beginners

  • Immutable by default: use let mut only for values that really change.
  • Statements end with a semicolon, but the last expression of a block, without one, is the block’s value.
  • println! is a macro, so it has an exclamation mark.
  • Unused variables give warnings, prefixed with an underscore to say that it is deliberate.
  • The compiler’s errors often include a suggested fix, so read them to the end.

Check by hand: the program prints 14 turbines, a wind speed of 11.5, and 2 alarms, because 0 + 2 is 2. The two file sizes differ because of optimisation and debug information, and the same ELF header appears in both because they are the same kind of file for the same system.

Do not reach for mut by habit

Start with let, and add mut only when the compiler says that you must. Fewer mutable values means fewer places where a bug can hide, and the compiler will tell you when one is needed.

Use cargo, and read the error codes

cargo new, cargo run and cargo test are the daily commands. An error code such as E0382 can be explained with rustc –explain E0382, which prints a short lesson with examples.

About this course

Every program in this lesson was compiled with rustc 1.97 (edition 2021) and run while the course was written. Rust releases every six weeks and its compiler messages improve all the time, so check doc.rust-lang.org for the current edition and wording.

Practice

Create a project with cargo new turbines and run it with cargo run. Change the numbers, add a line with your own message, and then remove the mut from alarms and read the compiler error. Build with cargo build –release and look at the file size in target/release.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What does mut do?

  1. Makes it faster
  2. Allows a variable to change
  3. Makes it public
  4. Deletes it

Variables are immutable by default.

What does the exclamation mark in println! mean?

  1. It is a macro
  2. It is an error
  3. It negates
  4. It is a comment

Macros can take format strings.

What is cargo?

  1. Rust’s build tool and package manager
  2. A compiler only
  3. A text editor
  4. A debugger

It builds, tests and fetches crates.

Which build should you use to measure speed?

  1. The debug build
  2. Either
  3. Neither
  4. The release build

cargo build –release.

What does rustc –explain E0382 do?

  1. Fixes the error
  2. Builds the code
  3. Formats the code
  4. Explains an error code

A short lesson.

Summary

  • Rust compiles to fast machine code and checks memory safety at compile time.
  • Variables are immutable unless declared with let mut.
  • cargo builds, runs and tests projects.
  • Use release builds to measure speed.

Next lesson

Lesson 2 covers types, shadowing and what happens on integer overflow.

Questions

Is Rust hard to learn?

The borrow checker takes time to get used to, and the compiler's messages are a good guide. Many learners find it rewarding once ownership clicks, which lesson 3 covers.

Do I need to install anything?

The rustup tool installs the compiler, cargo and the standard tools. Check rust-lang.org for the current instructions.

Why is Rust used?

For programs that must be fast and safe, such as browsers, operating system components, command-line tools and network services.

Progress is saved in this browser.