Level 1 · Foundations· Lesson 1 of 10 · 15 min
Getting started
What Rust is for, how to install it, and your first program built with Cargo.
Why Rust exists
Most languages make you choose. C and C++ give you control over memory and predictable performance, and in exchange a whole family of bugs — use-after-free, double free, data races, buffer overflows — is your responsibility. Java, Go, Python and JavaScript take those bugs away with a garbage collector and a runtime, and in exchange you give up control over when memory is freed and how data is laid out.
Rust’s claim is that you do not have to choose. It has no garbage collector and no mandatory runtime, yet programs written in safe Rust cannot have use-after-free bugs or data races. The compiler proves their absence before your program ever runs, using a set of rules called ownership that you will meet in lesson 4.
The price is paid at compile time: the compiler rejects programs that it cannot prove safe, and at first it will reject a lot of yours. This course is largely about learning to think the way the compiler does, until its rules feel like a description of good design rather than an obstacle.
Install the toolchain
Rust is installed with rustup, which manages compiler versions for you.
- Windows: download and run
rustup-init.exefrom rustup.rs. It will prompt you to install the Visual Studio C++ build tools if they are missing. - macOS / Linux: run the one-line installer shown on rustup.rs.
Then open a new terminal and check:
rustc --version
cargo --version
You now have three tools:
| Tool | Job |
|---|---|
rustup |
Installs and updates toolchains (rustup update) |
rustc |
The compiler. You will rarely call it directly |
cargo |
Build tool, package manager, test runner — your daily driver |
For an editor, install rust-analyzer (it is an extension for VS Code and most other editors). It shows types and errors as you type and is close to essential.
You can skip installation for now. Every example in this course has a Run button that compiles the code on the official Rust Playground. Install locally when you reach the first project.
Hello, world
fn main() {
println!("Hello, world!");
}
Press Run. Then press Edit, change the message, and run it again.
Three things to notice:
fn main()declares the function where every executable starts.println!has a!because it is a macro, not a function. Macros generate code at compile time; this one checks your format string against its arguments.- Statements end with semicolons, and blocks use braces. Indentation is four spaces by convention —
cargo fmtwill enforce it for you.
Your first Cargo project
cargo new hello
cd hello
cargo run
cargo new creates this layout:
hello/
├── Cargo.toml # package name, version, edition, dependencies
└── src/
└── main.rs # your code
The commands you will use every day:
| Command | What it does |
|---|---|
cargo check |
Type-check quickly, without building a binary |
cargo run |
Build and run (debug mode: fast to compile, slow to run) |
cargo run --release |
Build with optimisations |
cargo test |
Run tests |
cargo fmt |
Format the code in the standard style |
cargo clippy |
Lint: hundreds of checks for mistakes and unidiomatic code |
cargo doc --open |
Build and open documentation for your crate and its dependencies |
Printing values
println! uses {} placeholders. You can put a variable name directly inside the braces.
fn main() {
let language = "Rust";
let year = 2015;
println!("{language} 1.0 was released in {year}.");
println!("{} + {} = {}", 2, 3, 2 + 3);
println!("debug view: {:?}", (language, year));
println!("padded: [{:>8}] [{:<8}] [{:08.3}]", "right", "left", 3.14159);
}
{} is for user-facing output; {:?} is the debug format, which works on almost every type and is what you will use when investigating a problem.
Reading a compiler error
Run this. It fails on purpose.
fn main() {
let answer = 42;
println!("The answer is {anwser}"); // ❌ error[E0425]
}
Read the whole message. It has an error code (E0425), the exact location, and a suggestion with the name you probably meant. Rust’s diagnostics are written to be read, and you can get a longer explanation of any code with rustc --explain E0425. Make a habit of reading errors top to bottom before changing anything — the first error is usually the real one.
Exercises
- Change the hello-world program to print your name on one line and your favourite number on the next, using a variable for each.
- Print the value of
7 / 2and then7.0 / 2.0. Explain the difference to yourself before moving on (the next lesson confirms it). - Install Rust locally, run
cargo new playground, and runcargo clippyin it.
Solution to 2
fn main() {
println!("{}", 7 / 2); // 3 — both operands are integers, so this is integer division
println!("{}", 7.0 / 2.0); // 3.5 — floating-point division
}
Rust never converts between integer and floating-point types implicitly.
Check your understanding
Pick an answer to see the explanation.
1.Which tool do you use to create, build and run a Rust project?
2.Why does
println!end with an exclamation mark?3.What does
cargo checkdo?