-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
170 additions
and
471 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "std-types" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[[bin]] | ||
name = "hashset" | ||
path = "exercise.rs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,50 @@ | ||
--- | ||
minutes: 5 | ||
existing course material: | ||
- exercises/day-2/book-library.md | ||
--- | ||
|
||
<!-- NOTES: | ||
Inspired by example from https://doc.rust-lang.org/std/collections/struct.HashMap.html with one bit missing | ||
--> | ||
# Exercise: Book Reviews | ||
|
||
# Storing Books | ||
|
||
We will learn much more about structs and the `Vec<T>` type tomorrow. For now, | ||
you just need to know part of its API: | ||
|
||
```rust,editable | ||
fn main() { | ||
let mut vec = vec![10, 20]; | ||
vec.push(30); | ||
let midpoint = vec.len() / 2; | ||
println!("middle value: {}", vec[midpoint]); | ||
for item in &vec { | ||
println!("item: {item}"); | ||
} | ||
} | ||
``` | ||
|
||
Use this to model a library's book collection. Copy the code below to | ||
<https://play.rust-lang.org/> and update the types to make it compile: | ||
# Exercise: Hash Set | ||
|
||
```rust,should_panic | ||
{{#include exercise.rs:setup}} | ||
{{#include exercise.rs:Library_new}} | ||
todo!("Initialize and return a `Library` value") | ||
} | ||
In this exercise you will build a very simple hash set that stores `u32` | ||
values. The hash set will have a fixed size, and buckets should be selected | ||
with a simple modulus operation (`i % num_buckets`). Use a `Vec` to represent | ||
each bucket. | ||
|
||
{{#include exercise.rs:Library_len}} | ||
While solving this exercise, you may encounter a few "rough edges". The error | ||
messages from the compiler may help you to solve these, but if not, don't | ||
`panic!` -- discuss them with your classmates or instructor. | ||
|
||
{{#include exercise.rs:Library_is_empty}} | ||
```rust | ||
// TODO: define a type IntegerHashSet. | ||
struct IntegerHashSet; | ||
|
||
{{#include exercise.rs:Library_add_book}} | ||
{{#include exercise.rs:Library_print_books}} | ||
{{#include exercise.rs:new}} | ||
todo!() | ||
} | ||
|
||
{{#include exercise.rs:Library_oldest_book}} | ||
{{#include exercise.rs:test_membership}} | ||
todo!() | ||
} | ||
|
||
{{#include exercise.rs:main}} | ||
{{#include exercise.rs:tests}} | ||
``` | ||
|
||
If you finish early, adjust the hash set to use open chaining. What happens if | ||
all of the give integers do not fit? | ||
|
||
<details> | ||
|
||
[Solution](solutions-afternoon.md#designing-a-library) | ||
Highlight that students should not suffer the rough edges silently. | ||
|
||
* The hashset is passed by reference, but we haven't yet covered references. | ||
This is done in the provided code, so should not cause errors for students. | ||
|
||
* The integers are `u32` but the size of the hash table is a `usize`, so values must be cast. Are those casts correct? | ||
|
||
* Depending on how students implement iteration, they may run into ownership | ||
issues. For example, `for elt in hashset.buckets[b]` will move the bucket. | ||
The compiler will suggest `for elt in &hashset.buckets[b]`, but then `elt` | ||
has type `&u32`, so students must write `*elt`. | ||
* Consider this a kind of foreshadowing of memory and borrows on day 3. | ||
* The `Vec::contains` method may avoid the need for a `for` loop. | ||
|
||
</details> |
Oops, something went wrong.