Rust Get Started
You do not need to install anything to follow this course: every example on ExcelTAD compiles and runs on a real Rust compiler when you press ▶ Run. Still, sooner or later you will want Rust on your own computer, so this lesson shows how to set it up and how a real Rust project is organised.
Installing Rust with rustup
The official way to install Rust is rustup, a small tool that downloads the compiler and keeps it up to date.
On Linux or macOS, open a terminal and run:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shOn Windows, download and run rustup-init.exe from the official site rust-lang.org. It will also ask you to install the Visual Studio C++ build tools if they are missing.
When the installer finishes, open a new terminal and check that everything works:
rustc --version
cargo --versionBoth commands should print a version number. If they do, you are ready.
The tools you get
| Tool | What it does |
|---|---|
rustc |
The Rust compiler. Turns .rs source files into an executable program |
cargo |
Rust's build tool and package manager. Creates projects, builds, runs and tests them |
rustup |
Installs and updates the toolchain (rustup update) |
rustfmt |
Formats your code in the standard style (cargo fmt) |
clippy |
A linter that suggests better ways to write your code (cargo clippy) |
In practice you will almost always use cargo and rarely call rustc yourself.
Creating your first project
Cargo can create a ready-to-run project for you:
cargo new hello_rust
cd hello_rustThis creates the following layout:
hello_rust/
├── Cargo.toml project name, version and dependencies
└── src/
└── main.rs your program starts hereCargo.toml is a small configuration file:
[package]
name = "hello_rust"
version = "0.1.0"
edition = "2021"
[dependencies]And src/main.rs already contains a working program. Here it is — run it:
fn main() {
println!("Hello, world!");
}Hello, world!
Building and running
From inside the project folder, use these Cargo commands:
| Command | Effect |
|---|---|
cargo run |
Compile (if needed) and run the program |
cargo build |
Compile only; the executable goes into target/debug/ |
cargo build --release |
Compile with optimisations into target/release/ |
cargo check |
Check for errors very quickly without producing an executable |
A typical session looks like this:
$ cargo run
Compiling hello_rust v0.1.0 (/home/me/hello_rust)
Finished dev [unoptimized + debuginfo] target(s) in 0.52s
Running `target/debug/hello_rust`
Hello, world!The lines starting with Compiling, Finished and Running come from Cargo itself; only the last line is printed by your program.
Tip: Run
cargo checkoften while you write code. It finds mistakes in a fraction of the time a full build takes.
Editing the program
Open src/main.rs in any editor (VS Code with the rust-analyzer extension is a popular choice), change the code, and run cargo run again. Try it here as well — this version prints a small greeting card:
fn main() {
println!("+-------------------+");
println!("| Welcome to Rust |");
println!("+-------------------+");
}+-------------------+
| Welcome to Rust |
+-------------------+
Compiling without Cargo
For a single file you can call the compiler directly. This is handy for quick experiments:
rustc main.rs
./mainrustc produces an executable named after the file (main, or main.exe on Windows). For anything bigger than one file, prefer Cargo.
Note: Rust is a compiled language. Your code is translated into machine code before it runs, which is one reason Rust programs are so fast — but it also means errors are reported before the program ever starts.
Test yourself
Which command creates a new Rust project with a Cargo.toml and src/main.rs?
- rustc new
- cargo new
- rustup init
- cargo start
What does cargo check do?
- Runs the tests
- Formats the code
- Checks the code for errors without building an executable
- Updates Rust
Where does Cargo put the executable after cargo build --release?
- src/
- target/debug/
- target/release/
- bin/