Rust · Chapter 5 of 26
Ownership
By the end of this chapter you can say where a value lives (the stack or the heap), build text with String, tell String from &str, follow ownership's three rules line by line, say what a move is and why the compiler refuses a moved value (E0382), decide between moving and cloning, name the types that copy instead of moving, say when a value is freed, and give a value back from a function. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The language
- 4 tasks
- Rust
- In the all labs plan
The first 2 chapters of Rust are free. This one comes with the all labs plan.
Why you need this
The collection box builds text all day: a slip for every can (211 at Karamsad on Tuesday morning), and a shift report that grows by a line with every can and is sent to the plant at the end. That text is made while the program runs, and nobody knows its size in advance. Its memory must be freed once it is printed and sent: never twice, never while something still uses it, and never forgotten, on a small machine that runs from 6 am to 9 pm without a restart.
So far your text was written in the program itself, like "Boriavi", and format! made text you printed straight away. Chapter 1 said Rust has no garbage collector: no part of the program that walks memory looking for what can be freed. So who frees the text a slip is made of, and when? In Rust the answer is ownership: every value has one owner, the compiler follows the owner through every line, and frees the value when its owner is done with it. It is the idea everything after this chapter leans on.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
slip.rs: a slip, built as a String
Write pub fn slip(farmer: &str, ml: u32, paise: u64) -> String in slip.rs. It takes a farmer's id, a can's millilitres and what it earned in paise, and returns the slip as a String of three lines, each ending in a line break: Farmer: and the id, Milk: and the ml and ml, and Amount: ₹ with the rupees, a point and the paise as two digits. Build it with push_str (push may help too), using format! for the lines that hold numbers (the Check looks for both push_str and format!).
The Check calls slip("F10650", 3010, 10956) and expects "Farmer: F10650\nMilk: 3010 ml\nAmount: ₹109.56\n", slip("F12733", 5090, 17306) "Farmer: F12733\nMilk: 5090 ml\nAmount: ₹173.06\n", and slip("F10001", 0, 5) "Farmer: F10001\nMilk: 0 ml\nAmount: ₹0.05\n".
- Task 2
longer.rs: a report, given back longer
Write pub fn add_total(report: String, paise: u64) -> String in longer.rs. It takes a report, a String it now owns, and a total in paise, adds one line at the end of that same String, Total: ₹ with the rupees, a point, the paise as two digits and a line break, and gives the String back. It must be the same String made longer with push_str, not a new one: the Check looks at where the returned text lives, so a String made by format! with the report inside it fails.
The Check calls add_total with "Napad, morning\nF10650 109.56\n" and 12415897 and expects "Napad, morning\nF10650 109.56\nTotal: ₹124158.97\n", add_total("", 0) "Total: ₹0.00\n", and add_total("Boriavi\n", 5) "Boriavi\nTotal: ₹0.05\n".
- Task 3
copies.rs: two copies, one clone
The box prints every slip twice, one copy for the farmer and one for the society's file. Write pub fn two_copies(slip: String) -> (String, String) in copies.rs. It returns the slip as two Strings in a tuple, both with the slip's text unchanged, in either order. Make exactly one new String with clone, and move the original in as the other copy.
The Check passes a slip, checks that both Strings hold its text and that they are two different Strings, and looks at where each one's text lives: one must be the original's (moved, not copied). Two clones fail.
- Task 4 · interview question
Interview: report.rs, the report that was moved away
Lakhan wrote report.rs, a program of its own (cargo run --bin report), to show the shift report at S014 with a function show, then add a line and show it again. It does not compile. Fix it so that it prints exactly:
Report, S014 morning
Cans: 114
Report, S014 morning
Cans: 114
Closed at 08:54
Fix it without clone (the Check looks): the move was the mistake, not the lack of a copy. Keep fn show and both calls to it, and keep the line that pushes Cans: 114 as it is: the Check also runs report.rs with Cans: 97 in that line, and then both reports must say Cans: 97.
Farhan will ask: why will the compiler not let main use report after show(report), and why is giving it back better than clone here?
What you will learn
- The stack and the heap: two places a program keeps values.
- String and &str: text you own and can grow, and text you only look at.
- Ownership's three rules: one owner, and freed when the owner goes.
- Moves: what let b = a; does to a String, and error E0382.
- clone and Copy: a real copy and what it costs, and the types that copy for free.
- Giving ownership back from a function.
More in The language
Build "Ownership" yourself
DoodhSetu runs the milk collection boxes of 120 village societies around Anand. The old box program crashed on an empty line, and you rebuild it in Rust. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Rust are free. This one comes with the all labs plan.