Rust Get Started
Let’s get Rust running on your machine and build your first program. If you’d rather not install anything yet, you can follow along in the Rust Playground and come back to this page later.
Step 1: Install Rust with rustup
Section titled “Step 1: Install Rust with rustup”The official way to install Rust is a tool called rustup. It installs the Rust compiler, the cargo build tool, and keeps everything up to date.
Go to rustup.rs and follow the instructions for your system:
-
Windows — download and run
rustup-init.exe, then accept the default options. -
macOS / Linux — paste this into your terminal:
Terminal window curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Step 2: Check it worked
Section titled “Step 2: Check it worked”Close and reopen your terminal, then run:
rustc --versioncargo --versionYou should see version numbers like rustc 1.xx.0. If you do, you’re ready to go.
rustcis the Rust compiler (it turns your code into a program).cargois Rust’s all-in-one tool for building, running, testing, and managing projects. You’ll usecargofor almost everything.
Step 3: Create your first project
Section titled “Step 3: Create your first project”Instead of creating files by hand, let cargo set up a project for you:
cargo new hello_rustcd hello_rustThis creates a folder called hello_rust with everything a Rust project needs:
hello_rust/├── Cargo.toml # project settings and dependencies└── src/ └── main.rs # your code goes hereCargo.tomldescribes your project (its name, version, and any libraries it uses).src/main.rsis the starting file —cargoeven fills it with a Hello World for you.
Step 4: Run it
Section titled “Step 4: Run it”From inside the project folder, run:
cargo runYou’ll see cargo compile the project and then print:
Hello, world!That’s it — you just built and ran a Rust program.
Understanding the code
Section titled “Understanding the code”Open src/main.rs and you’ll find:
fn main() { println!("Hello, world!");}fn main() { ... }defines the main function — every Rust program starts running here.println!("...")prints a line of text to the screen.
Try changing the message and running cargo run again:
fn main() { println!("Hello from my first Rust program!");}Output:
Hello from my first Rust program!cargo run vs cargo build
Section titled “cargo run vs cargo build”Two commands you’ll use constantly:
| Command | What it does |
|---|---|
cargo run |
Compiles and runs your program (best while developing) |
cargo build |
Just compiles it — the program lands in target/debug/ |
cargo build --release |
Compiles an optimized, faster version for shipping |
When something goes wrong: reading compiler errors
Section titled “When something goes wrong: reading compiler errors”You will see compiler errors, every day, even after years of Rust. They’re not a sign you’re bad at this. They’re the compiler doing its job. Let’s make one on purpose so it’s less scary the first time.
Remove the ! from println! in src/main.rs:
fn main() { println("Hello, world!");}Run cargo run and you’ll get:
error[E0423]: expected function, found macro `println` --> src/main.rs:2:5 |2 | println("Hello, world!"); | ^^^^^^^ not a function |help: use `!` to invoke the macro |2 | println!("Hello, world!"); | +Read it from top to bottom:
- The first line says what’s wrong: Rust expected a function, but
printlnis a macro (you’ll learn what that means on the next page). --> src/main.rs:2:5tells you where: filesrc/main.rs, line 2, column 5.- The
^^^^^^^marks point at the exact piece of code. - The
help:part often shows the fix. Here it even marks the missing!with a+.
Put the ! back and everything works again:
fn main() { println!("Hello, world!");}Output:
Hello, world!Step 5: Set up your editor (recommended)
Section titled “Step 5: Set up your editor (recommended)”For the best experience, install the rust-analyzer extension in your editor. It gives you autocompletion, inline error messages, and type hints as you type.
- VS Code — install the rust-analyzer extension
- Most other editors (JetBrains, Neovim, etc.) support rust-analyzer too.
Practice
Section titled “Practice”1. Three lines
Section titled “1. Three lines”Make your program print three lines: your name, your favorite food, and I just ran Rust!. Hint: one println! prints one line, so use three of them.
Show solution
fn main() { println!("My name is Sam"); println!("I like pizza"); println!("I just ran Rust!");}Output:
My name is SamI like pizzaI just ran Rust!2. A tiny banner
Section titled “2. A tiny banner”Print a small banner like the one below, with a line of = signs above and below a title. Then break it on purpose by deleting a !, run cargo run, read the error, and fix it. Hint: the = lines are just text inside println!, like any other message.
Show solution
fn main() { println!("=============="); println!(" HELLO RUST"); println!("==============");}Output:
============== HELLO RUST==============- Install Rust with
rustupfrom rustup.rs. - Verify with
rustc --versionandcargo --version. - Create a project with
cargo new, run it withcargo run. - A project has a
Cargo.toml(settings) andsrc/main.rs(your code). - Read compiler errors top to bottom: what, where, and often a
help:with the fix. - Install rust-analyzer in your editor for a much smoother ride.
Quick check
Section titled “Quick check”Quick check
1. What tool installs the Rust compiler, cargo, and keeps them updated?
2. Which file describes a Rust project's name, version, and dependencies?
3. Which command only compiles your program without running it?
Score: 0 / 3
Read more
Section titled “Read more”- Installation — The Rust Book: installing on every platform, updating with
rustup update, and troubleshooting. - Hello, Cargo! — The Rust Book: a closer look at
cargo new,cargo build,cargo runandcargo check. - First Steps with Cargo — The Cargo Book: what
cargo newcreates and what each line ofCargo.tomlmeans. - The rustup book: installing extra toolchains, updating Rust, and switching between stable and nightly.
- rust-analyzer: setup guides for VS Code, Neovim, Emacs and other editors.