Rust · Chapter 10 of 26
Vec and HashMap
By the end of this chapter you can keep any number of values in a Vec, loop over it to read, change or use it up, sort and filter it in place, count and total by key with a HashMap, keep things in order with a BTreeMap or a BTreeSet, and work with a whole day of DoodhSetu's data from the doodh crate. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Traits and data
- 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
On Tuesday 13 October, 4,773 farmers poured milk 10,164 times at 120 societies. Neha needs, by tonight: how much each society collected in each shift, each society's best-paid farmers for its notice board, and the list of societies that rejected a can, for Lakhan's visits tomorrow.
Chapter 9 counted as it went, with one variable per number: cans, ml, broken. That works for one shift at one counter. For 120 societies it would take 120 variables, and the list of farmers at a society is not known until the data arrives. Arrays (chapter 3) have a length fixed when the program is compiled, [u32; 7], so they cannot grow as pourings come in. You need a list that grows, and a way to look a value up by a key such as "S014" instead of by position.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
by_society.rs: millilitres per society for a shift
Write ml_by_society(pourings: &[Collection], shift: Shift) -> HashMap<String, u64>. It returns a map from each society's id to the total ml of its accepted pourings in that shift, as a u64. Pourings of the other shift, and rejected pourings, do not count, and a society with no accepted pouring in that shift is not in the map at all.
The Check runs it on Tuesday's pourings from doodh::collections() for both shifts and compares every society: for the morning there are 120, with S001 1931010 ml, S014 627430 ml and S120 405500 ml; for the evening S001 has 1499720 ml. It also tries a short list it makes, with two societies, a rejected can and both shifts, and an empty list, which must give an empty map.
- Task 2
top.rs: a society's top five farmers
Write top_five(pourings: &[Collection], society: &str) -> Vec<(String, u64)>. It adds up the paise each farmer of that society earned over the whole day (both shifts; a rejected pouring has paise 0), and returns the five who earned most as (farmer id, paise) pairs, most first. Farmers with equal paise go in order of id, the smaller id first. With fewer than five farmers it returns them all, and for a society with no pourings an empty Vec.
The Check tries Tuesday's pourings for S001 (its top five start with ("F10147", 170133), then ("F10150", 162666)), S014, S120 and S999 (an empty Vec), and a short list it makes for S027 in which F11243 and F11250 both have 15000 paise and must come in that order.
- Task 3
rejected.rs: the societies with a rejected can, in order
Write with_rejections(pourings: &[Collection]) -> BTreeSet<String>. It returns the ids of every society that had at least one rejected pouring, each id once. A BTreeSet keeps its items sorted, so S003 comes before S014.
On Tuesday's pourings the Check expects exactly the societies with a rejected can: 107 of them, starting with S001, S002 and S003 (S011 and S012 had none). It also tries a short list it makes, in which S014 has two rejected cans, S002 one and S001 none, and expects S002 and S014; and an empty list, which must give an empty set.
- Task 4 · interview question
Interview: report.rs, the report that copied the day
report.rs has day_report(pourings: Vec<Collection>) -> String, which returns one line: 10164 pourings, 82698770 ml, 300 rejected for Tuesday (the number of pourings, the total ml of all of them, and how many were rejected). Lakhan's first try did not compile: after one for loop over pourings, the next loop and pourings.len() said borrow of moved value (E0382). So he cloned the Vec for each loop, which copies all 10,164 pourings twice every time the report runs.
Fix it so nothing is copied, keeping its name and its parameter (a Vec it owns). The Check reads report.rs, which must have no clone in it, and calls day_report on Tuesday's pourings (the line above) and on a short list of 8 pourings it makes (8 pourings, 38700 ml, 3 rejected).
Farhan will ask: why did for c in pourings move pourings, and what does a for loop over &pourings do instead?
What you will learn
- The doodh crate: DoodhSetu's societies, farmers and Tuesday's pourings, as Rust values.
- Vec: a list that grows: vec!, push, len, [i] and get.
- for over &v, &mut v and v: read it, change it, or use it up.
- Changing a Vec: sort, sort_by_key, dedup, contains, retain, extend.
- HashMap: values by key: insert, get, entry and or_insert, contains_key, remove.
- BTreeMap, HashSet and BTreeSet: keys in order, and sets of unique items.
More in Traits and data
Build "Vec and HashMap" 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.