Project · Rust Programming
Project: stock register for a medical shop
The brief
Build the stock register of a neighbourhood medical shop. Medicines are structs with an enum for their form (tablet, syrup, cream); a stock file is parsed line by line into a Vec with a custom error type; a HashMap gives the stock value per form; iterators build the reorder list; a trait works out GST on a sale (example rates); and finally the code moves into its own module.
Why it matters
Pharmacies, kirana stores and warehouses all run on the same core: typed records, input that is often wrong, totals by category, low-stock alerts and a sale that must fail cleanly when stock is short. This project uses the exact Rust tools a backend or tools developer reaches for every day: struct, enum, match, Result with ?, Vec, HashMap, iterator chains, traits and modules.
Skills you practise
- struct and impl
- enum with match
- custom error enum with Display
- Result, ? and ok_or_else
- Vec and HashMap with entry()
- iterator chains: filter, map, sum, sort_by_key
- a trait with a default method
- mod and pub across files
You need
- Rust stable installed with rustup
- A terminal and an editor (VS Code with rust-analyzer)
What you will have at the end
A medstock cargo project with src/stock.rs (types, errors, trait and functions) and a short main.rs that loads stock, reports bad lines, values the stock, lists reorders and sells with GST.
How it is marked (rubric, 100 points)
| Correct resultsLoad counts, error lines, stock values, reorder order, sale lines, bill and remaining stock all match. | 35 |
|---|---|
| Types model the shopForm is an enum handled with match; StockError has one variant per problem with useful data and a Display message. | 25 |
| Idiomatic RustResult and ? instead of unwrap, entry() for totals, iterator chains, &[T] and &mut [T] parameters, a trait with a default method. | 25 |
| Organised codeA stock module with deliberate pub items, a short main.rs, no warnings. | 15 |
0/6 steps done
Step 1 of 6
A Medicine struct and a Form enum
Three medicines built by hand print with their unit names: strips, bottles and tubes.
Create the project with cargo new medstock and copy the starter. Define enum Form { Tablet, Syrup, Cream } and give it a method unit(&self) -> &'static str that uses match to return "strip", "bottle" or "tube"; match must cover every variant, so adding a new form later forces you to handle it. Define struct Medicine with name: String, form: Form, stock: u32 and price_paise: u32. In main, make three medicines (Paracetamol 500 mg, tablet, 120, 2500; Cough syrup 100 ml, syrup, 15, 9500; Antiseptic cream 20 g, cream, 40, 6000) and print each one.
$ cargo runYour notes: src/main.rs
Write down each command you ran. It goes into your project summary.