typestar

Language basics

39 steps in 8 sets of Rust.

Rust before the hard parts. Variables, the type system, compound values, control flow, functions, text, and the collections you will use most. Pattern matching gets its own set because match is not a switch statement and treating it like one wastes the best thing in the language.

Thirty-nine steps, and none of it fights you yet. Ownership is the next tour, and it will.

Start this tour

Variables & types

Compound values

  • TuplesFixed-size groups of mixed types.
  • ArraysFixed-size arrays live on the stack and carry their length in the type.
  • Tuple and unit structsStructs without field names: newtypes for meaning, unit structs for markers.
  • DestructuringPatterns pull structs, tuples and nested values apart in one binding.

Control flow

  • if / else if / elseBranching where every branch is an expression.
  • LoopsRust's three loop forms: for, while, and loop.
  • RangesRanges are values: iterate them, reverse them, step through them.
  • Loops that return valuesloop is an expression: break carries a value out, and labels pick which loop to leave.
  • while letwhile let keeps looping as long as the pattern matches — draining a stack, say.
  • let elselet else binds on the happy path and forces the failure branch to diverge.
  • Everything is an expressionBlocks, ifs and matches all evaluate to a value, which is why Rust needs few statements.
  • FizzBuzzThe classic screening exercise, written with match rather than if.

Pattern matching

Functions

  • FunctionsDefining, composing, and passing functions.
  • GCDEuclid's algorithm, still the shortest correct answer after two thousand years.
  • Iterative FibonacciFibonacci without recursion, and a first look at shadowing.
  • Constants and type aliasesCompile-time constants, program-lifetime statics, and names for long types.

Text

  • Format stringsThe formatting mini-language: width, precision, alignment, debug output.
  • Parsing textTurning strings into numbers, and deciding what a failure means.
  • Building stringsGrowing a String, joining pieces, and the cheap ways to slice text.

Collections

  • VectorsThe growable Vec and its everyday methods.
  • Working a VecThe Vec methods that do real work: filtering in place, sorting by key, windows.
  • Sorting and searchingSorting by a comparator, finding extremes, and searching a sorted slice.
  • Binary searchHalve the range each step, with the index arithmetic the borrow checker is happy about.
  • The entry APIOne lookup instead of two: entry gives you the slot, occupied or not.
  • HashSetMembership and set algebra, with no duplicate values by construction.
  • BTreeMapAn ordered map: iteration follows the keys, and you can ask for a range.
  • VecDequeA ring buffer: push and pop at either end in constant time.

Encore

  • temps.rsConvert Celsius temperatures from arguments to Fahrenheit.
  • grades.rsA gradebook: structs, a HashMap of results, and a formatted report.

The other Rust tours