@@ -83,18 +83,25 @@ body.
83
83
expect an iterator</span >
84
84
85
85
The standard library documentation for the ` env::args ` function shows that the
86
- type of the iterator it returns is ` std::env::Args ` . We’ve updated the
87
- signature of the ` Config::new ` function so the parameter ` args ` has the type
88
- ` std::env::Args ` instead of ` &[String] ` . Because we’re taking ownership of
89
- ` args ` and we’ll be mutating ` args ` by iterating over it, we can add the ` mut `
90
- keyword into the specification of the ` args ` parameter to make it mutable.
86
+ type of the iterator it returns is ` std::env::Args ` , and that type implements
87
+ the ` Iterator ` trait and returns ` String ` values.
88
+
89
+ We’ve updated the signature of the ` Config::new ` function so the parameter
90
+ ` args ` has a generic type with the trait bounds ` impl Iterator<Item = String> `
91
+ instead of ` &[String] ` . This usage of the ` impl Trait ` syntax we discussed in
92
+ the [ “Traits as Parameters”] [ impl-trait ] <!-- ignore --> section of Chapter 10
93
+ means that ` args ` can be any type that implements the ` Iterator ` type and
94
+ returns ` String ` items.
95
+
96
+ Because we’re taking ownership of ` args ` and we’ll be mutating ` args ` by
97
+ iterating over it, we can add the ` mut ` keyword into the specification of the
98
+ ` args ` parameter to make it mutable.
91
99
92
100
#### Using ` Iterator ` Trait Methods Instead of Indexing
93
101
94
- Next, we’ll fix the body of ` Config::new ` . The standard library documentation
95
- also mentions that ` std::env::Args ` implements the ` Iterator ` trait, so we know
96
- we can call the ` next ` method on it! Listing 13-27 updates the code from
97
- Listing 12-23 to use the ` next ` method:
102
+ Next, we’ll fix the body of ` Config::new ` . Because ` args ` implements the
103
+ ` Iterator ` trait, we know we can call the ` next ` method on it! Listing 13-27
104
+ updates the code from Listing 12-23 to use the ` next ` method:
98
105
99
106
<span class =" filename " >Filename: src/lib.rs</span >
100
107
@@ -165,3 +172,5 @@ the iterator must pass.
165
172
But are the two implementations truly equivalent? The intuitive assumption
166
173
might be that the more low-level loop will be faster. Let’s talk about
167
174
performance.
175
+
176
+ [ impl-trait ] : ch10-02-traits.html#traits-as-parameters
0 commit comments