@@ -6,16 +6,17 @@ its lifetime shorter:
66{{#rustdoc_include .. / code / ch01 - 01 - building - an - intuition / lifetime - shortener / src / main . rs: all }}
77```
88
9+ <!-- Note: all the code sample names begin with "cell" rather than "hash-set" as you might expect. This is because hash sets should -->
10+
911Intuitively, this feels like it should compile: if a string lasts for the whole
1012process it should also last for any part of it. And it does!
1113
12- Now let's make it slightly more complicated. Let's introduce a ` Cell ` into the
13- picture. As a reminder, a ` Cell ` allows for the data inside it to be changed.
14+ Now let's make it slightly more complicated. Let's put some strings into a ` HashSet ` .
1415``` rust,does_not_compile
1516{{#rustdoc_include ../code/ch01-01-building-an-intuition/cell-shortener/src/main.rs:all}}
1617```
1718
18- ` cell_shortener ` doesn't compile :( Can you tell why? Think about it for a minute,
19+ ` hash_set_shortener ` doesn't compile :( Can you tell why? Think about it for a minute,
1920try using your intuition...
2021``` rust,does_not_compile
2122{{#rustdoc_include ../code/ch01-01-building-an-intuition/cell-example/src/main.rs:all}}
@@ -25,16 +26,16 @@ try using your intuition...
2526{{#rustdoc_include ../code/ch01-01-building-an-intuition/cell-counterexample/src/main.rs:all}}
2627```
2728
28- It isn't just ` Cell ` which is problematic in this way. ` RefCell ` , ` OnceCell ` ,
29- ` Mutex ` , ` &mut ` references -- anything " inside" some sort of mutable context has
30- this issue.
29+ It isn't just ` &mut ` which is problematic in this way. This also occurs with any sort of interior
30+ mutability, like ` RefCell ` , ` OnceCell ` , or ` Mutex ` -- anything inside some sort of mutable context
31+ has this issue.
3132
3233Now, what about a hypothetical "lengthener" function?
3334``` rust,does_not_compile
3435{{#rustdoc_include ../code/ch01-01-building-an-intuition/lifetime-lengthener/src/main.rs:all}}
3536```
3637
37- This is obviously bogus, right? You can't just turn an arbitrary borrowed string
38+ This is clearly bogus, right? You can't just turn an arbitrary borrowed string
3839and make it last the duration of the entire process. Similarly:
3940``` rust,does_not_compile
4041{{#rustdoc_include ../code/ch01-01-building-an-intuition/cell-lengthener/src/main.rs:all}}
@@ -46,5 +47,8 @@ borrowed string.
4647{{#rustdoc_include .. / code / ch01 - 01 - building - an - intuition / fn - ptr - lengthener / src / main . rs: all }}
4748```
4849
49- Ahhh, intuitively, this should work. And it does. You can take a callback that
50- takes an arbitrary borrowed string and turn it into one that takes in a static string.
50+ This feels like should work. You can take a callback that takes an arbitrary borrowed string and
51+ turn it into one that takes in a static string, since you're weakening the guarantee. And it does.
52+
53+ How can we handle these different cases in a principled way? That's where variance comes in. We're
54+ going to talk about this in the next chapter, * [ Formalizing variance] ( ch01-02-formalizing-variance.md ) * .
0 commit comments