I don't care about the variant of the enum.
I just need to gather the single or many strings to a single Vec<&str> depending on the variant of the enum. Like this,
// path: &IOPath
let paths = match path {
IOPath::Single(p) => {
vec![p.as_str()]
}
IOPath::Dynamic(p) => {
p.iter().map(|s| s.as_str()).collect() // <-- first iteration
}
};
for path in paths { // <-- second iteration
let path: Path = path.as_ref();
}
Because I am just iterating over paths I don't want to iterate over it twice.
thanks, alice. I did consider it doing this way.
But I was wondering if there is an obvious way (without using an external crate), that I am missing (being new to rust).
No quite. May be my phrasing was confusing. I was wondering if there is any other (easy) way.
But I realize, this is Rust, coming from python, it is a very different world.
In the original solution, I used the Iterator::map method, which has the type of the closure as part of the Map<Self, F> return type (it's the F). In Rust, every single closure has its own unique type, which is what allows the compiler to optimize code using iterators down to the same assembly as the corresponding loop. However, the types of these closures do not have a name that is usable in the source code, and since it is part of the type of Map, this makes it impossible to specify the IntoIter associated type if you are using Map.
To get around this, I reimplemented the Map iterator from the standard library in the MyCustomIterator type, and hardcoded it to call as_str() instead of a user-provided closure. This allows me to give the IntoIter associated type a name. Note that the map used in my custom iterator is not the same as the one I used previously. In this case, it is Option::map.
The reason using Iterator::map is fine with the fn iter approach I initially suggested is that in the return type of functions, the impl Trait syntax allows me to say "this returns some type that implements Iterator" without saying which one, sidestepping the no-name issue of using closures.
This feature cannot be used in associated types, however.