问题
I need to collect an iterator over a slice of &str
s into a collection of &str
s. The problem is that the iterator yields &&str
s.
I tried to map from &word
to word
, and while it works, I don't know if it is considered good or if there are better options available.
The problem:
use std::collections::HashSet;
fn main() {
let words = &["hello", "world", "I'm", "a", "Rustacean!"];
let hashset = words.iter().collect::<HashSet<&str>>();
}
Playground
error[E0277]: a collection of type `std::collections::HashSet<&str>` cannot be built from an iterator over elements of type `&&str`
--> src/main.rs:5:32
|
5 | let hashset = words.iter().collect::<HashSet<&str>>();
| ^^^^^^^ a collection of type `std::collections::HashSet<&str>` cannot be built from `std::iter::Iterator<Item=&&str>`
|
= help: the trait `std::iter::FromIterator<&&str>` is not implemented for `std::collections::HashSet<&str>`
My solution:
use std::collections::HashSet;
fn main() {
let words = &["hello", "world", "I'm", "a", "Rustacean!"];
let hashset = words.iter().map(|&w| w).collect::<HashSet<&str>>();
}
Playground
回答1:
If T
implements Copy
then you can use Iterator::copied. Otherwise, if it implements Clone
then you can use Iterator::cloned. Like any immutable reference, &str
implements both Clone
and Copy
, so you can use copied
:
let hashset: HashSet<&str> = words.iter().copied().collect();
回答2:
Is a bicycle an idiomatic way to get from one city to another? Like most things in software (and life), it depends.
If your type implements Copy
I'd prefer these in this order:
some_iter.copied()
some_iter.cloned()
some_iter.map(|&v| v)
some_iter.map(|v| *v)
some_iter.map(Clone::clone)
some_iter.map(|v| v.clone())
If your type implements Clone
I'd prefer these in this order:
some_iter.cloned()
some_iter.map(Clone::clone)
some_iter.map(|v| v.clone())
If your type doesn't implement Copy
or Clone
Then you cannot trivially create an owned value. The type may implement ToOwned or there may be a bespoke function or method that you can call inside of map
, or you may simply not be able to do anything.
In your case, I'd use words.iter().copied().collect::<HashSet<_>>()
.
See also:
- Iterating over a slice's values instead of references in Rust?
- What's the idiomatic way to copy from a primitive type reference by value?
- Why is "&&" being used in closure arguments?
- Why does the argument for the find closure need two ampersands?
来源:https://stackoverflow.com/questions/57593717/what-is-an-idiomatic-way-to-collect-an-iterator-of-t-into-a-collection-of-ts