Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

reference - What is an idiomatic way to collect an iterator of &T into a collection of Ts?

I need to collect an iterator over a slice of &strs into a collection of &strs. The problem is that the iterator yields &&strs.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  1. some_iter.copied()
  2. some_iter.cloned()
  3. some_iter.map(|&v| v)
  4. some_iter.map(|v| *v)
  5. some_iter.map(Clone::clone)
  6. some_iter.map(|v| v.clone())

If your type implements Clone

I'd prefer these in this order:

  1. some_iter.cloned()
  2. some_iter.map(Clone::clone)
  3. 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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...