RsRust

Learn Rust – tutorial, cheatsheet & exercises

Rust is a systems programming language that combines maximum speed with memory safety, and it does so without a garbage collector. The ownership system makes this possible: at compile time the compiler checks who owns a value and who may borrow it. Whole classes of bugs such as use-after-free, double free, or data races simply cannot occur.

Rust is used for operating system components, browser engines, WebAssembly, command-line tools (ripgrep, bat), network services, and embedded systems. The learning curve is steep because the compiler is strict, but its error messages are exceptionally helpful, and once your program compiles, many bugs never show up at runtime. The Cargo tool (build, packages, tests) makes everyday work very pleasant.

Systems programmingWebAssemblyCLI toolsEmbedded

Hello World in Rust

fn main() {
    println!("Hello World!");
}

main is the entry point, println! is a macro (recognizable by the !). Run it with cargo run or rustc hallo.rs.

How to get started with Rust

Install Rust via rustup, the official installer and toolchain manager (rustup.rs shows the command for your system; on Windows there is an installer). On a Mac and on Linux, this is all you need in the terminal:

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

Then open a new terminal and check that the compiler (rustc) and the build tool (cargo) are available:

rustc --version
cargo --version
rustup update      # updates the toolchain

You can compile a single program directly with rustc. For anything serious, use Cargo: cargo new creates a project with Cargo.toml and src/main.rs, and cargo run builds and starts it.

# Single file
rustc hallo.rs
./hallo

# Project with Cargo
cargo new hallo
cd hallo
cargo run
TipA good editor choice is VS Code with the rust-analyzer extension (autocompletion, live errors, type hints). cargo check verifies your code quickly without building a binary, and cargo clippy gives style tips.
TipTo try things out without installing anything, there is the Rust Playground at play.rust-lang.org in your browser.

At a glance

First released2010
CreatorGraydon Hoare (Mozilla)
ParadigmMulti-paradigm (functional, procedural)
TypingStatic, strong
ExecutionCompiled to machine code
File extension.rs

Test & practice

Interactive quiz, timed exam with certificate, and exercises with an editor:

Rust compared

Hello World: Rust comparedVariables: Rust comparedConditionals: Rust comparedLoops: Rust comparedFunctions: Rust comparedLists and Arrays: Rust comparedDictionaries and Maps: Rust comparedClasses and Objects: Rust comparedError Handling: Rust comparedReading Files: Rust compared