ZetCode

Rust tutorials

Rust is a compiled systems programming language focused on safety, speed, and concurrency. Its ownership system and borrow checker guarantee memory safety at compile time, without a garbage collector, and rule out data races in multithreaded code. Rust has zero-cost abstractions such as traits, generics, closures, and iterators, a rich type system with enums and pattern matching, and explicit error handling with the Option and Result types. It is used for command-line tools, web services, embedded systems, and WebAssembly.

Rust toolchain

Rust is installed with rustup, which manages the compiler rustc, the build tool and package manager Cargo, and the standard library. Libraries, called crates, come from the crates.io registry and are added to a project with cargo add. The examples in these tutorials use Rust 1.97 and the 2024 edition.

Getting started

The introduction installs Rust, compiles and runs programs with rustc and Cargo, and reads user input. Cargo creates projects, adds dependencies, uses debug and release profiles, and organizes crates in a workspace. The variables and mutability articles cover type inference, shadowing, constants, statics, and mutable references, and operators and expressions explains arithmetic, bitwise and range operators, casts, and integer overflow. Control flow uses if, loop, while, for, labels, and let else, and functions passes parameters and references, returns values, and uses recursion and function pointers.

Ownership and memory

Ownership and borrowing is the core idea of the language: moves, clones, shared and mutable references, and how the borrow checker prevents dangling references. Slices borrow parts of arrays, vectors, and strings. Lifetimes explains lifetime annotations in functions and structs, the elision rules, and the 'static lifetime. Smart pointers covers Box, the Deref and Drop traits, shared ownership with Rc and Arc, interior mutability with RefCell, and Weak references.

Data types

Structs defines named, tuple, and unit structs with methods in impl blocks, and enums attaches data to variants and builds recursive types. Tuples groups and destructures values, and arrays works with fixed-size sequences. Strings explains String and &str and how UTF-8 affects indexing, and string formatting covers format!, width, precision, number bases, and the Display and Debug traits. The int to string and type conversion articles convert values with as, From and Into, TryFrom, and parse.

Pattern matching and error handling

The match expression matches literals, ranges, enums, tuples, and structs, and pattern matching shows every place patterns appear: if let, let chains, while let, guards, @ bindings, and slice patterns. The Option type handles missing values, and unwrap explains unwrap and expect and the safer alternatives. Error handling uses Result, the ? operator, custom error types, and error source chains.

Traits, generics, and functional style

Traits defines shared behaviour with default methods, trait bounds, trait objects, associated types, and blanket implementations. Generics writes generic functions, structs, and enums with where clauses, the turbofish syntax, and const generics. Closures captures the environment and explains the Fn, FnMut, and FnOnce traits. Iterators chains adapters such as map, filter, and zip and writes custom iterators, and collect turns iterators into vectors, strings, maps, sets, and results.

Collections

Vectors creates, modifies, searches, and transforms Vec<T> and explains capacity. HashMap stores key-value pairs and uses the entry API, and HashSet and BTreeMap covers set operations, ordered maps, and range queries. Sorting sorts vectors and slices by keys and comparators, sorts floats, and implements Ord.

Modules and testing

Modules and crates organizes code with mod, pub, and use, splits modules into files, and combines a library crate with a binary. Testing writes unit tests with #[test] and the assert macros, integration tests, and doc-tests.

Concurrency

Threads spawns and joins threads, uses scoped threads, and shares state with Arc, Mutex, RwLock, and atomics. Channels passes messages between threads with std::sync::mpsc, worker pools, and pipelines. Async and Tokio runs futures on the Tokio runtime with join!, spawn, select!, and timeouts.

Files and the command line

Reading files uses fs::read_to_string, BufReader, and seeking, and writing to files creates, appends to, and safely saves files with OpenOptions and BufWriter. Directories and paths builds paths with Path and PathBuf and creates, lists, walks, and removes directories. Command-line arguments reads std::env::args and builds interfaces with the clap crate.

Popular crates

Serde JSON converts structs to JSON and back and works with untyped Value data. Regular expressions matches, captures, replaces, and splits text with the regex crate. Random numbers uses the rand crate for numbers, samples, shuffling, and distributions, and the HTTP client sends requests with reqwest: JSON bodies, headers, timeouts, and concurrent downloads.

Every example in these tutorials is compiled and run with Rust 1.97 before its output is shown.

last modified November 25, 2026