Skip to content

Commit

Permalink
Edit the std-types segment
Browse files Browse the repository at this point in the history
  • Loading branch information
djmitche committed Sep 20, 2023
1 parent aa72269 commit 70b5e9d
Show file tree
Hide file tree
Showing 17 changed files with 175 additions and 471 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"src/language-features",
"src/tuples-and-arrays",
"src/user-defined-types",
"src/std-types",
"src/exercises",
"src/bare-metal/useful-crates/allocator-example",
"src/bare-metal/useful-crates/zerocopy-example",
Expand Down
6 changes: 3 additions & 3 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@
- [Exercise: Expression Evaluation](user-defined-types/exercise.md)
- [Solution](user-defined-types/solution.md)
- [Standard Library Types](std-types.md)
- [Standard Library](std-types/std.md)
- [Option](std-types/option.md)
- [Result](std-types/result.md)
- [String](std-types/string.md)
- [Vec](std-types/vec.md)
- [HashMap](std-types/hashmap.md)
- [Exercise: Book Reviews](std-types/exercise.md)
- [Exercise: Hash Set](std-types/exercise.md)
- [Solution](std-types/solution.md)

----

Expand Down Expand Up @@ -291,8 +293,6 @@
----

- [Solutions](exercises/solutions.md)
- [Day 1 Afternoon](exercises/day-1/solutions-afternoon.md)
- [Day 2 Morning](exercises/day-2/solutions-morning.md)
- [Day 2 Afternoon](exercises/day-2/solutions-afternoon.md)
- [Day 3 Morning](exercises/day-3/solutions-morning.md)
- [Day 3 Afternoon](exercises/day-3/solutions-afternoon.md)
Expand Down
4 changes: 0 additions & 4 deletions src/exercises/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ publish = false
name = "luhn"
path = "day-1/luhn.rs"

[[bin]]
name = "book-library"
path = "day-2/book-library.rs"

[[bin]]
name = "strings-iterators"
path = "day-2/strings-iterators.rs"
Expand Down
13 changes: 0 additions & 13 deletions src/exercises/day-1/solutions-afternoon.md

This file was deleted.

185 changes: 0 additions & 185 deletions src/exercises/day-2/book-library.rs

This file was deleted.

9 changes: 0 additions & 9 deletions src/exercises/day-2/solutions-morning.md

This file was deleted.

2 changes: 1 addition & 1 deletion src/std-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ In this segment:
* [String](std-types/string.md)
* [Vec](std-types/vec.md)
* [HashMap](std-types/hashmap.md)
* [Exercise: Book Reviews](std-types/exercise.md)
* [Exercise: Hash Set](std-types/exercise.md)
9 changes: 9 additions & 0 deletions src/std-types/Cargo.toml
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"
78 changes: 39 additions & 39 deletions src/std-types/exercise.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
---
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 (separate chaining). Hint: a vector can contain other vectors:
`Vec<Vec<u32>>`.

{{#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, try to adjust the hash set to use open addressing. What
happens if all of the give integers do not fit?

<details>

[Solution](solutions-afternoon.md#designing-a-library)
The rough edges are present partially because it's hard to do "interesting"
things in Rust without references, and partially because responding to the Rust
compiler's errors is an important skill to practice.

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. Can we be sure those casts are 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>
Loading

0 comments on commit 70b5e9d

Please sign in to comment.