Skip to content

Commit 13b97ab

Browse files
committed
fix hash set example
1 parent 6ce558c commit 13b97ab

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

code/ch01-01-building-an-intuition/cell-counterexample/src/main.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ fn hash_set_shortener<'a, 'b>(s: &'a mut HashSet<&'static str>) -> &'a mut HashS
99

1010
// ANCHOR: all
1111
fn hash_set_counterexample() {
12-
let mut foo: HashSet<&'static str> = HashSet::from_iter(["static"]);
12+
let mut my_set: HashSet<&'static str> = HashSet::from_iter(["static"]);
1313
let owned_string: String = "non_static".to_owned();
14-
15-
// If we pretend that hash_set_shortener works
16-
let shorter_foo = hash_set_shortener(&mut foo);
17-
18-
// then `shorter_foo` and `foo` would be aliases of each other, which would
19-
// mean that you could use `shorter_foo` to insert a non-static string:
20-
shorter_foo.replace(&owned_string);
21-
22-
// Now `foo`, which is an alias of `shorter_foo`, has a non-static string
23-
// in it! Whoops.
14+
15+
// If we pretend that hash_set_shortener works...
16+
let shorter_set = hash_set_shortener(&mut my_set);
17+
18+
// then you could use `shorter_set` to insert a non-static string:
19+
shorter_set.insert(&owned_string);
20+
21+
// Now we can drop `shorter_set` to regain the ability to use `my_set`:
22+
std::mem::drop(shorter_set);
23+
24+
// And my_set now has a non-static string in it. Whoops!
2425
}
2526
// ANCHOR_END: all
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
#![allow(dead_code)]
22
fn main() {}
33

4-
use std::cell::Cell;
4+
use std::collections::HashSet;
5+
use std::iter::FromIterator;
56

67
// ANCHOR: all
7-
fn cell_example() {
8-
// Consider this Cell. It holds a static string.
9-
let foo: Cell<&'static str> = Cell::new("foo");
8+
fn hash_set_example() {
9+
// Consider this HashSet over static strings.
10+
let mut my_set: HashSet<&'static str> = HashSet::from_iter(["static"]);
1011

1112
// Do you think this can work?
1213
let owned_string: String = "non_static".to_owned();
13-
foo.replace(&owned_string);
14+
my_set.insert(&owned_string);
1415

15-
// Doesn't seem like it can, right? foo promises that what's inside it is
16-
// a &'static str, but we tried to put in an owned string scoped to this
16+
// Doesn't seem like it can, right? my_set promises that the &strs inside it
17+
// are all 'static, but we tried to put in an owned string scoped to this
1718
// function.
1819
}
19-
// ANCHOR_END: all
20+
// ANCHOR_END: all

code/ch01-01-building-an-intuition/cell-shortener/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ fn main() {}
44
use std::collections::HashSet;
55

66
// ANCHOR: all
7-
fn hash_set_shortener<'a, 'b>(s: &'a mut HashSet<&'static str>) -> &'a mut HashSet<&'b str> {
7+
fn hash_set_shortener<'a, 'b>(
8+
s: &'a mut HashSet<&'static str>,
9+
) -> &'a mut HashSet<&'b str> {
810
s
911
}
1012

src/ch01-01-building-an-intuition.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,22 @@ its lifetime shorter:
1111
Intuitively, this feels like it should compile: if a string lasts for the whole
1212
process it should also last for any part of it. And it does!
1313

14-
Now let's make it slightly more complicated. Let's put some strings into a `HashSet`.
14+
Now let's make it a bit more complicated. Consider a mutable reference to a `HashSet`.
15+
1516
```rust,does_not_compile
1617
{{#rustdoc_include ../code/ch01-01-building-an-intuition/cell-shortener/src/main.rs:all}}
1718
```
1819

19-
`hash_set_shortener` doesn't compile :( Can you tell why? Think about it for a minute,
20-
try using your intuition...
20+
`hash_set_shortener` doesn't compile!
21+
22+
Can you tell why? Think about it for a minute, try using your intuition...
23+
2124
```rust,does_not_compile
2225
{{#rustdoc_include ../code/ch01-01-building-an-intuition/cell-example/src/main.rs:all}}
2326
```
2427

28+
As a counterexample:
29+
2530
```rust,does_not_compile
2631
{{#rustdoc_include ../code/ch01-01-building-an-intuition/cell-counterexample/src/main.rs:all}}
2732
```

0 commit comments

Comments
 (0)