Skip to content

Commit 4275162

Browse files
committed
Explain that we do need to have 'static now
1 parent 0b81c3b commit 4275162

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/ch13-03-improving-our-io-project.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ signature of the `Config::new` function so the parameter `args` has the type
8989
`args` and we’ll be mutating `args` by iterating over it, we can add the `mut`
9090
keyword into the specification of the `args` parameter to make it mutable.
9191

92+
We also needed to specify that the string slice error type can now only have
93+
the `'static` lifetime. Because we’re only ever returning string literals, this
94+
was true before. However, when we had a reference in the parameters, there was
95+
the possibility that the reference in the return type could have had the same
96+
lifetime as the reference in the parameters. The rules that we discussed in the
97+
[“Lifetime Elision”][lifetime-elision] section of Chapter 10 applied, and we
98+
weren’t required to annotate the lifetime of `&str`. With the change to `args`,
99+
the lifetime elision rules no longer apply, and we must specify the `'static`
100+
lifetime.
101+
92102
#### Using `Iterator` Trait Methods Instead of Indexing
93103

94104
Next, we’ll fix the body of `Config::new`. The standard library documentation
@@ -165,3 +175,5 @@ the iterator must pass.
165175
But are the two implementations truly equivalent? The intuitive assumption
166176
might be that the more low-level loop will be faster. Let’s talk about
167177
performance.
178+
179+
[lifetime-elision]: ch10-03-lifetime-syntax.html#lifetime-elision

0 commit comments

Comments
 (0)