Skip to content

Commit e4583e2

Browse files
committed
Edit the std-types segment
1 parent 2c1b511 commit e4583e2

17 files changed

+170
-471
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ members = [
55
"src/language-features",
66
"src/tuples-and-arrays",
77
"src/user-defined-types",
8+
"src/std-types",
89
"src/exercises",
910
"src/bare-metal/useful-crates/allocator-example",
1011
"src/bare-metal/useful-crates/zerocopy-example",

src/SUMMARY.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@
6161
- [Exercise: Expression Evaluation](user-defined-types/exercise.md)
6262
- [Solution](user-defined-types/solution.md)
6363
- [Standard Library Types](std-types.md)
64+
- [Standard Library](std-types/std.md)
6465
- [Option](std-types/option.md)
6566
- [Result](std-types/result.md)
6667
- [String](std-types/string.md)
6768
- [Vec](std-types/vec.md)
6869
- [HashMap](std-types/hashmap.md)
69-
- [Exercise: Book Reviews](std-types/exercise.md)
70+
- [Exercise: Hash Set](std-types/exercise.md)
71+
- [Solution](std-types/solution.md)
7072

7173
----
7274

@@ -291,8 +293,6 @@
291293
----
292294

293295
- [Solutions](exercises/solutions.md)
294-
- [Day 1 Afternoon](exercises/day-1/solutions-afternoon.md)
295-
- [Day 2 Morning](exercises/day-2/solutions-morning.md)
296296
- [Day 2 Afternoon](exercises/day-2/solutions-afternoon.md)
297297
- [Day 3 Morning](exercises/day-3/solutions-morning.md)
298298
- [Day 3 Afternoon](exercises/day-3/solutions-afternoon.md)

src/exercises/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ publish = false
88
name = "luhn"
99
path = "day-1/luhn.rs"
1010

11-
[[bin]]
12-
name = "book-library"
13-
path = "day-2/book-library.rs"
14-
1511
[[bin]]
1612
name = "strings-iterators"
1713
path = "day-2/strings-iterators.rs"

src/exercises/day-1/solutions-afternoon.md

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/exercises/day-2/book-library.rs

Lines changed: 0 additions & 185 deletions
This file was deleted.

src/exercises/day-2/solutions-morning.md

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/std-types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ In this segment:
77
* [String](std-types/string.md)
88
* [Vec](std-types/vec.md)
99
* [HashMap](std-types/hashmap.md)
10-
* [Exercise: Book Reviews](std-types/exercise.md)
10+
* [Exercise: Hash Set](std-types/exercise.md)

src/std-types/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "std-types"
3+
version = "0.1.0"
4+
edition = "2021"
5+
publish = false
6+
7+
[[bin]]
8+
name = "hashset"
9+
path = "exercise.rs"

src/std-types/exercise.md

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,50 @@
11
---
22
minutes: 5
3-
existing course material:
4-
- exercises/day-2/book-library.md
53
---
64

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
316

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.
3711

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.
3915

40-
{{#include exercise.rs:Library_is_empty}}
16+
```rust
17+
// TODO: define a type IntegerHashSet.
18+
struct IntegerHashSet;
4119

42-
{{#include exercise.rs:Library_add_book}}
43-
44-
{{#include exercise.rs:Library_print_books}}
20+
{{#include exercise.rs:new}}
21+
todo!()
22+
}
4523

46-
{{#include exercise.rs:Library_oldest_book}}
24+
{{#include exercise.rs:test_membership}}
25+
todo!()
4726
}
4827

49-
{{#include exercise.rs:main}}
28+
{{#include exercise.rs:tests}}
5029
```
5130

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+
5234
<details>
5335

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.
5549

5650
</details>

0 commit comments

Comments
 (0)