TExcelTAD
Home › Rust › Rust Get Started

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:

bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

On 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:

bash
rustc --version
cargo --version

Both 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:

bash
cargo new hello_rust
cd hello_rust

This creates the following layout:

text
hello_rust/
├── Cargo.toml      project name, version and dependencies
└── src/
    └── main.rs     your program starts here

Cargo.toml is a small configuration file:

toml
[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:

rust · try it yourself
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:

bash
$ 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 check often 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:

rust · try it yourself
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:

bash
rustc main.rs
./main

rustc 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?

  1. rustc new
  2. cargo new
  3. rustup init
  4. cargo start

What does cargo check do?

  1. Runs the tests
  2. Formats the code
  3. Checks the code for errors without building an executable
  4. Updates Rust

Where does Cargo put the executable after cargo build --release?

  1. src/
  2. target/debug/
  3. target/release/
  4. bin/