Using the Returned Iterator Directly
Open your I/O project's src/main.rs
file, which should look like this:
Filename: src/main.rs
fn main() {
let args: Vec<String> = env::args().collect();
let config = Config::build(&args).unwrap_or_else(|err| {
eprintln!("Problem parsing arguments: {err}");
process::exit(1);
});
--snip--
}
We'll first change the start of the main
function that we had in Listing 12-24 to the code in Listing 13-18, which this time uses an iterator. This won't compile until we update Config::build
as well.
Filename: src/main.rs
fn main() {
let config =
Config::build(env::args()).unwrap_or_else(|err| {
eprintln!("Problem parsing arguments: {err}");
process::exit(1);
});
--snip--
}
Listing 13-18: Passing the return value of env::args
to Config::build
The env::args
function returns an iterator! Rather than collecting the iterator values into a vector and then passing a slice to Config::build
, now we're passing ownership of the iterator returned from env::args
to Config::build
directly.
Next, we need to update the definition of Config::build
. In your I/O project's src/lib.rs
file, let's change the signature of Config::build
to look like Listing 13-19. This still won't compile, because we need to update the function body.
Filename: src/lib.rs
impl Config {
pub fn build(
mut args: impl Iterator<Item = String>,
) -> Result<Config, &'static str> {
--snip--
Listing 13-19: Updating the signature of Config::build
to expect an iterator
The standard library documentation for the env::args
function shows that the type of the iterator it returns is std::env::Args
, and that type implements the Iterator
trait and returns String
values.
We've updated the signature of the Config::build
function so the parameter args
has a generic type with the trait bounds impl Iterator<Item = String>
instead of &[String]
. This usage of the impl Trait
syntax we discussed in "Traits as Parameters" means that args
can be any type that implements the Iterator
type and returns String
items.
Because we're taking ownership of args
and we'll be mutating args
by iterating over it, we can add the mut
keyword into the specification of the args
parameter to make it mutable.