Lab · Rust Programming
Lab: who owns the kirana order?
The brief
A kirana shop writes each home-delivery order as a String: one copy stays at the counter, one goes with the delivery rider. You make Rust's ownership rules fail on purpose, read the two E0382 messages (borrow of a moved value, use of a moved value), and fix them with clone and by passing ownership into a function and back out.
Why it matters
Ownership is the idea every Rust interview asks about and the first wall every new Rust developer hits. Seeing exactly which line moved a value, and choosing between clone, returning ownership or (next module) borrowing, is a skill you use every day in real Rust code.
Skills you practise
- moves of String
- error E0382
- clone() versus Copy types
- ownership into and out of functions
- returning a tuple to hand values back
You need
- Rust stable installed with rustup
- A terminal and an editor
What you will have at the end
An order_slips cargo project that prints delivery slips correctly, plus the two E0382 messages you produced and fixed.
How it is marked (rubric, 100 points)
| Correct outputBoth slips, the item counts and the final 4-item slip print exactly. | 35 |
|---|---|
| Understands movesCan explain each E0382 message: where the value moved and why String moves but u32 copies. | 35 |
| Right fix for the jobclone only where two independent copies are needed; ownership returned from functions instead of cloning everywhere. | 20 |
| Clean codeNo warnings, clear function names. | 10 |
0/4 steps done
Step 1 of 4
Move the order and read E0382
You have pasted error E0382 "borrow of moved value: `order`".
Create the project with cargo new order_slips. Write let delivery_slip = order; and then print order and delivery_slip. A String owns heap memory, so assigning it MOVES it: delivery_slip is now the only owner and order cannot be used. Run cargo run and paste the error. Find the three labels in the message: where order was created (and why it moves: String is not Copy), "value moved here", and "value borrowed here after move" (println! only borrows the value to print it).
$ cargo runYour notes: src/main.rs
Write down each command you ran. It goes into your project summary.