|
1 | 1 | /// A struct that represents split operations on a string.
|
2 |
| -/// |
3 |
| -/// TODO(student): You will need to change the `SplitPattern` struct to use lifetime parameter(s). |
4 |
| -pub struct SplitPattern<P> { |
| 2 | +pub struct SplitPattern<'a, P> { |
5 | 3 | /// The remainder of the string that has not yet been split.
|
6 | 4 | ///
|
7 | 5 | /// Before the iterator has yielded any substrings, this is the entire string.
|
8 | 6 | /// After each call to `next`, this is the part of the string that has not yet been split.
|
9 |
| - /// |
10 |
| - /// TODO(student): Replace the `'static` lifetime with something else! |
11 |
| - remainder: Option<&'static str>, |
| 7 | + remainder: Option<&'a str>, |
12 | 8 |
|
13 | 9 | /// The generic delimiter pattern used to split the haystack string.
|
14 | 10 | delimiter: P,
|
15 | 11 | }
|
16 | 12 |
|
17 |
| -impl<P> SplitPattern<P> { |
| 13 | +impl<'a, P> SplitPattern<'a, P> { |
18 | 14 | /// Creates a new `Split` instance with the given haystack and delimiter.
|
19 |
| - /// |
20 |
| - /// TODO(student): Replace the `'static` lifetime with something else! |
21 |
| - pub fn new(haystack: &'static str, delimiter: P) -> Self { |
| 15 | + pub fn new(haystack: &'a str, delimiter: P) -> Self { |
22 | 16 | Self {
|
23 | 17 | remainder: Some(haystack),
|
24 | 18 | delimiter,
|
25 | 19 | }
|
26 | 20 | }
|
27 | 21 | }
|
28 | 22 |
|
29 |
| -impl<P> Iterator for SplitPattern<P> |
| 23 | +impl<'a, P> Iterator for SplitPattern<'a, P> |
30 | 24 | where
|
31 | 25 | P: Pattern,
|
32 | 26 | {
|
33 | 27 | /// This iterator yields substrings of the original `haystack` string, split by some delimiter
|
34 | 28 | /// pattern.
|
35 |
| - /// |
36 |
| - /// TODO(student): Replace the `'static` lifetime with something else! |
37 |
| - type Item = &'static str; |
| 29 | + type Item = &'a str; |
38 | 30 |
|
39 | 31 | /// Returns the next substring of the original `haystack` string, split by some delimiter
|
40 | 32 | /// pattern.
|
41 | 33 | ///
|
42 | 34 | /// Panics if the delimiter is empty (length 0).
|
43 | 35 | fn next(&mut self) -> Option<Self::Item> {
|
44 |
| - todo!("Implement me (make sure to fix the lifetimes!)") |
| 36 | + todo!("Implement me!") |
45 | 37 | }
|
46 | 38 | }
|
47 | 39 |
|
|
0 commit comments