This repository contains my progress and exercises as I work through "The Rust Programming Language" book. Each directory represents a specific topic or project from the book.
1. Hello Cargo
- Path:
hello_cargo/src/main.rs - Description: A basic "Hello, world!" program created using Cargo, Rust's build system and package manager.
- Path:
guessing_game/src/main.rs - Description: An interactive CLI game where the program generates a random number between 1 and 100, and the player tries to guess it. It demonstrates
std::io,randcrate,matchexpressions, and loops.
- Path:
variables/src/main.rs - Description: Explores Rust's core concepts of variable mutability, shadowing, and basic data types including scalars (integers, floats, booleans, characters) and compounds (tuples, arrays).
4. Functions
- Path:
functions/src/main.rs - Description: Demonstrates how to define functions with parameters and return values. It also highlights the distinction between statements and expressions in Rust.
- Path:
branches/src/main.rs - Description: Covers basic control flow using
if,else if, andelsestatements, including usingifin aletstatement.
6. Loops
- Path:
loops/src/main.rs - Description: Demonstrates various looping constructs:
loop,while, andfor. Includes examples of returning values from loops and using loop labels to disambiguate nested loops.
7. Ownership
- Path:
ownership/src/main.rs - Description: Deep dive into Rust's most unique feature: ownership. Covers stack vs. heap memory, variable scope, the
Stringtype, moving, cloning, and how ownership works with functions.
- Path:
references_and_borrowing/src/main.rs - Description: Explores the concepts of references and borrowing, allowing data access without taking ownership. Covers immutable and mutable references, data race prevention, and string slices (&str).
9. Structs
- Path:
structs/src/main.rs - Description: Covers Chapter 5 of the book — Structs. Demonstrates
defining basic structs (e.g. User), tuple structs (e.g. Color, Point),
adding methods with
impl, associated functions as constructors, and debug printing with{:?}.
10. Rectangles
- Path:
structs/rectangles/src/main.rs - Description: A worked example from Chapter 5 — calculates the area
of a rectangle using a struct. Demonstrates refactoring from plain
variables to tuples to a named struct, and using
implto add anareamethod directly on the Rectangle struct.
- Path:
enums_and_pattern_matching/src/main.rs - Description: Covers Chapter 6 of the book — Enums and Pattern Matching.
Demonstrates defining enums with various types of associated data,
implementing methods on enums, using the
Optionenum for null safety, and exhaustive pattern matching withmatch. Also coversif let,let else, and using_as a catch-all pattern.
- Path:
packages_crates_modules/src/main.rsandpackages_crates_modules/restaurant/src/lib.rs - Description: Covers Chapter 7 of the book — Managing Growing Projects with Packages, Crates, and Modules. Demonstrates organizing code using modules, controlling visibility with the
pubkeyword, using absolute and relative paths (includingsuperandcrate), and bringing paths into scope with theusekeyword. It also covers re-exporting withpub use, using external packages, and the multi-file module system.
- Path:
collections/src/main.rs - Description: Covers Chapter 8 of the book — Common Collections. Explores the three most common collections in Rust:
- Vectors (
Vec<T>): Storing lists of values, handling indices safely withget, and the borrow checker's role in preventing memory errors. - Strings: Understanding UTF-8 encoding, why direct indexing is disallowed, and efficient ways to concatenate and iterate over text.
- Hash Maps: Storing key-value pairs, managing ownership, and various strategies for updating values (including
entryandor_insert).
- Vectors (
14. Error Handling
- Path:
error_handling/src/main.rs - Description: Covers Chapter 9 of the book — Error Handling. Explores Rust's approach to both unrecoverable and recoverable errors:
- Unrecoverable Errors with
panic!: Understanding when to usepanic!, how to read backtraces withRUST_BACKTRACE, and configuring panic behavior (unwinding vs. aborting) inCargo.toml. - Recoverable Errors with
Result: Working with theResult<T, E>enum, using pattern matching to handle success/failure, and theunwrapandexpectshortcuts. - Propagating Errors: Using the
?operator for concise error propagation, custom error types with theFromtrait for automatic conversion, and returningResultfrommainusingBox<dyn Error>. - Guidelines for Error Handling: Best practices for when to use
panic!vs.Result, and using the "Newtype" pattern to enforce invariants at the type level.
- Unrecoverable Errors with
To run any of the projects, navigate into its directory and use Cargo:
cd <project_directory>
cargo runFor example:
cd guessing_game
cargo runLearning Rust, one step at a time!