Rust from scratch

Cargo, variables and mutability, control flow as expressions, functions and loops — the foundations before ownership.

Rust promises the speed of C and C++ without their memory bugs, and it keeps that promise with a strict compiler. The first meeting with that compiler can be bumpy, so this course takes the basics slowly: how a Rust project is built, why variables cannot change unless you say so, why if produces a value, and why a missing semicolon can be the difference between a function that compiles and one that does not.

The repository covers the fundamentals so far — the five small Cargo projects under basics/. Ownership and borrowing, Rust’s defining idea, come next.

Prerequisites

Some programming experience in any language. Comparisons are drawn with C and Python where they help.

Running the code

Install the toolchain with rustup, then run any of the projects with Cargo:

cd rust_from_scratch/basics/loops
cargo run

Lessons

  1. 1 Hello, world and CargoCompiling a single file with rustc, then letting Cargo manage the project, the build and the binary.
  2. 2 Variables, mutability and constantsImmutable by default, typed by inference, and checked for overflow — Rust's variables are stricter than they look.
  3. 3 Control flowConditions must be booleans, the first true branch wins, and if is an expression that produces a value.
  4. 4 Functions, statements and expressionsTyped parameters, return types, and why the last line of a function has no semicolon.
  5. 5 Loops and rangesloop, while and for — and the half-open range that stops one short.

Browse this course's code on GitHub →