|
1 | 1 | --- |
2 | 2 | minutes: 5 |
3 | | -existing course material: |
4 | | -- exercises/day-2/book-library.md |
5 | 3 | --- |
6 | 4 |
|
7 | | -<!-- NOTES: |
8 | | -Inspired by example from https://doc.rust-lang.org/std/collections/struct.HashMap.html with one bit missing |
9 | | ---> |
10 | | -# Exercise: Book Reviews |
11 | | - |
12 | | -# Storing Books |
13 | | - |
14 | | -We will learn much more about structs and the `Vec<T>` type tomorrow. For now, |
15 | | -you just need to know part of its API: |
16 | | - |
17 | | -```rust,editable |
18 | | -fn main() { |
19 | | - let mut vec = vec![10, 20]; |
20 | | - vec.push(30); |
21 | | - let midpoint = vec.len() / 2; |
22 | | - println!("middle value: {}", vec[midpoint]); |
23 | | - for item in &vec { |
24 | | - println!("item: {item}"); |
25 | | - } |
26 | | -} |
27 | | -``` |
28 | | - |
29 | | -Use this to model a library's book collection. Copy the code below to |
30 | | -<https://play.rust-lang.org/> and update the types to make it compile: |
| 5 | +# Exercise: Hash Set |
31 | 6 |
|
32 | | -```rust,should_panic |
33 | | -{{#include exercise.rs:setup}} |
34 | | -{{#include exercise.rs:Library_new}} |
35 | | - todo!("Initialize and return a `Library` value") |
36 | | - } |
| 7 | +In this exercise you will build a very simple hash set that stores `u32` |
| 8 | +values. The hash set will have a fixed size, and buckets should be selected |
| 9 | +with a simple modulus operation (`i % num_buckets`). Use a `Vec` to represent |
| 10 | +each bucket. |
37 | 11 |
|
38 | | -{{#include exercise.rs:Library_len}} |
| 12 | +While solving this exercise, you may encounter a few "rough edges". The error |
| 13 | +messages from the compiler may help you to solve these, but if not, don't |
| 14 | +`panic!` -- discuss them with your classmates or instructor. |
39 | 15 |
|
40 | | -{{#include exercise.rs:Library_is_empty}} |
| 16 | +```rust |
| 17 | +// TODO: define a type IntegerHashSet. |
| 18 | +struct IntegerHashSet; |
41 | 19 |
|
42 | | -{{#include exercise.rs:Library_add_book}} |
43 | | -
|
44 | | -{{#include exercise.rs:Library_print_books}} |
| 20 | +{{#include exercise.rs:new}} |
| 21 | + todo!() |
| 22 | +} |
45 | 23 |
|
46 | | -{{#include exercise.rs:Library_oldest_book}} |
| 24 | +{{#include exercise.rs:test_membership}} |
| 25 | + todo!() |
47 | 26 | } |
48 | 27 |
|
49 | | -{{#include exercise.rs:main}} |
| 28 | +{{#include exercise.rs:tests}} |
50 | 29 | ``` |
51 | 30 |
|
| 31 | +If you finish early, adjust the hash set to use open chaining. What happens if |
| 32 | +all of the give integers do not fit? |
| 33 | + |
52 | 34 | <details> |
53 | 35 |
|
54 | | -[Solution](solutions-afternoon.md#designing-a-library) |
| 36 | +Highlight that students should not suffer the rough edges silently. |
| 37 | + |
| 38 | + * The hashset is passed by reference, but we haven't yet covered references. |
| 39 | + This is done in the provided code, so should not cause errors for students. |
| 40 | + |
| 41 | + * The integers are `u32` but the size of the hash table is a `usize`, so values must be cast. Are those casts correct? |
| 42 | + |
| 43 | + * Depending on how students implement iteration, they may run into ownership |
| 44 | + issues. For example, `for elt in hashset.buckets[b]` will move the bucket. |
| 45 | + The compiler will suggest `for elt in &hashset.buckets[b]`, but then `elt` |
| 46 | + has type `&u32`, so students must write `*elt`. |
| 47 | + * Consider this a kind of foreshadowing of memory and borrows on day 3. |
| 48 | + * The `Vec::contains` method may avoid the need for a `for` loop. |
55 | 49 |
|
56 | 50 | </details> |
0 commit comments