Skip to content

Commit 4bfc9ed

Browse files
committed
Use impl Iterator instead of std::env::Args
Fixes #2021. Fixes #3052.
1 parent 4284e16 commit 4bfc9ed

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

listings/ch13-functional-features/listing-13-26/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ pub struct Config {
1010

1111
// ANCHOR: here
1212
impl Config {
13-
pub fn new(mut args: env::Args) -> Result<Config, &'static str> {
13+
pub fn new(
14+
mut args: impl Iterator<Item = String>,
15+
) -> Result<Config, &'static str> {
1416
// --snip--
1517
// ANCHOR_END: here
1618
if args.len() < 3 {

listings/ch13-functional-features/listing-13-27/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ pub struct Config {
1010

1111
// ANCHOR: here
1212
impl Config {
13-
pub fn new(mut args: env::Args) -> Result<Config, &'static str> {
13+
pub fn new(
14+
mut args: impl Iterator<Item = String>,
15+
) -> Result<Config, &'static str> {
1416
args.next();
1517

1618
let query = match args.next() {

listings/ch13-functional-features/listing-13-29/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ pub struct Config {
99
}
1010

1111
impl Config {
12-
pub fn new(mut args: std::env::Args) -> Result<Config, &'static str> {
12+
pub fn new(
13+
mut args: impl Iterator<Item = String>,
14+
) -> Result<Config, &'static str> {
1315
args.next();
1416

1517
let query = match args.next() {

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

+18-9
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,25 @@ body.
8383
expect an iterator</span>
8484

8585
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.
9199

92100
#### Using `Iterator` Trait Methods Instead of Indexing
93101

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:
98105

99106
<span class="filename">Filename: src/lib.rs</span>
100107

@@ -165,3 +172,5 @@ the iterator must pass.
165172
But are the two implementations truly equivalent? The intuitive assumption
166173
might be that the more low-level loop will be faster. Let’s talk about
167174
performance.
175+
176+
[impl-trait]: ch10-02-traits.html#traits-as-parameters

0 commit comments

Comments
 (0)