This repository contains a collection of exercises designed to learn and practice the fundamental concepts of the Rust programming language.
The project is structured as a library (lib.rs) with multiple modules, separating definitions from the main execution loop and integration tests.
A function that takes a reference to a vector of strings and transposes them. The output vector strings are formed by grouping the n-th characters of each input string. The output strings' length is bounded by the shortest string in the input vector.
A module implementing rational numbers (Rational).
- Core Logic: Fractions are automatically reduced to their lowest terms upon creation using the Greatest Common Divisor (GCD).
- Methods: Sum, product, inverse, and comparison.
- Trait Implementation: Standard library traits are heavily used here to make the struct behave like primitive numbers. Includes custom implementations for
std::ops::Add,std::ops::Mul,std::fmt::Display, andstd::cmp::PartialOrd, alongside derived traits likeDebugandPartialEq.
A 2D terminal-based game simulation.
- The Grid: An
n x nmatrix surrounded by walls. Inner cells can be empty, contain food (increases strength), or poison (decreases strength). - The Player: Starts with a given strength and a random direction (Up, Down, Left, Right).
- Mechanics: Each turn, a coin is flipped. Heads means the player keeps moving in the current direction; Tails means the player picks a new random direction. Hitting a wall makes the player bounce in the opposite direction.
- Win/Loss: The game ends in victory if the player survives for a maximum number of moves, or in defeat if their strength drops to 0 or below.
An object-oriented approach to a Vending Machine using Rust's Trait Objects to implement the State Pattern.
- Context: The
DistributoreAutomaticoholds an inventory (HashMap<String, i32>), the currently selected product, and its current State. - States: Transitions between
InAttesaCarta,CartaAccettata,SelezionatoProdotto,ProdottoEsaurito, andProdottoDisponibile. - Actions: Depending on the current state, actions like inserting a card, selecting a product, confirming, or canceling will have different behaviors and outputs.
A genealogy model that explores Rust's smart pointers for building graph-like, mutable, cyclic data structures.
- The Nodes: Each
Personholds a name plus optional references to amother,father, and a vector ofchildren. Shared ownership across the tree is handled withRc<RefCell<Person>>—Rcallows multiple owners (a person is referenced both as a parent's child and as its own node), whileRefCellenables interior mutability so relationships can be wired up after creation. - The Collection:
Peoplewraps a vector of persons and exposes the registry API:add_person(rejects duplicate names viaResult),search_person,set_mother,set_father, andprint(falls back to "unknown" when a parent is missing). - Traversal:
descendantswalks the tree recursively, using a seen-list guard to ensure no name is ever produced twice.
A concurrent implementation of the English Auction protocol where each actor is its own thread.
- Actors: One auctioneer thread plus
nparticipant threads, each spawned independently. - Message Passing: Communication runs entirely over
mpscchannels — participants sendPartecipantMessage(Ready,Bid,Exit) to the auctioneer, while the auctioneer broadcastsAuctioneerMessage(Start,NewPrice,Winner) back to every active participant. - Protocol Flow: After all participants signal
Ready, the auctioneer announces the product and starting price. Each round, participants either bid above the current price or drop out; every valid raise is broadcast to the remaining bidders. The auction ends once all participants have exited. - Outcome: The item is sold only if the final price clears the
reserve_price. The result (product, final price, winning participant) is written exactly once at the end into anArc<Mutex<AuctionOutcome>>, the single piece of shared state read by participants — keeping with the rule that channels drive the protocol and shared memory is used only for the final result.
The main executable (main.rs) currently runs the English Auction simulation (Exercise 7). The Game module (Exercise 3) and earlier main setups are kept as commented-out blocks you can swap back in.
cargo runAll modules are strictly tested using Rust's built-in testing framework. Tests are separated into their own directory (tests/).
To run the entire test suite:
cargo test