Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd `Iterator::collect_into<E: Extend>(e: E)`? #45840
Comments
This comment has been minimized.
This comment has been minimized.
|
The problem is that Another possibility is You can also do this -- but note that type SizeHint = (usize, Option<usize>);
struct OverrideSizeHint<I>(I, SizeHint);
extension_trait! {
<I: Iterator> SizeHintExt<I> for I {
fn override_size_hint(self, hint: SizeHint) -> OverrideSizeHint<I> {
OverrideSizeHint(self, hint)
}
}
}
impl<I: Iterator> Iterator for OverrideSizeHint<I> {
type Item = I::Item;
fn next(&mut self) -> Option<I::Item> {
self.0.next()
}
fn size_hint(&self) -> SizeHint {
self.1
}
}
fn collect_hint(ratio: u8, chars: &[char]) -> Vec<char> {
chars.iter().enumerate().filter_map(|(i, c)| {
if filter(ratio, i) {
Some(*c)
} else {
None
}
}).override_size_hint((chars.len(), Some(chars.len()))).collect()
} |
This comment has been minimized.
This comment has been minimized.
|
Hmm, what about
|
This comment has been minimized.
This comment has been minimized.
|
@scottmcm : I like that alternative as well. |
kennytm
added
C-feature-request
T-libs
labels
Nov 27, 2017
This comment has been minimized.
This comment has been minimized.
|
I noticed the other day that unzip has a similar problem. It always just extends with |
stuhood
changed the title
`collect_with_size_hint`?
Add `Iterator::collect_into<E: Extend>(e: E)`?
Nov 29, 2017
This comment has been minimized.
This comment has been minimized.
|
Updated description/title to apply @scottmcm 's |
This comment has been minimized.
This comment has been minimized.
|
Do any trait Iterator {
/* ... */
fn collect_into<E: Extend<Self::Item>>(self, e: E) -> E {
e.extend(self);
e
}
}As I understand it, the following are always equivalent right? let vec = whatever.into_iter().etc(...).collect_into(Vec::with_capacity(N));let mut vec = Vec::with_capacity(N);
vec.extend(whatever.into_iter().etc(...));Is the advantage of |
This comment has been minimized.
This comment has been minimized.
Yes... and avoiding the mutable local variable is nice as well. Finally, the symmetry with |
This comment has been minimized.
This comment has been minimized.
CodeSandwich
commented
Jan 27, 2018
•
|
I love the idea of collecting into an existing collection, but for another reason: ergonomics. I've proposed something similar on Rust user and internals forums. I think, that the function ( |
cuviper
referenced this issue
Jan 29, 2018
Closed
Consider renaming `IndexedParallelIterator::collect_into` #519
This comment has been minimized.
This comment has been minimized.
|
We might also consider Also, FWIW rayon already has |
This comment has been minimized.
This comment has been minimized.
CodeSandwich
commented
Feb 16, 2018
|
I feel that @stuhood I've included your motivation, are you OK with that? |
stuhood commentedNov 7, 2017
•
edited
Hey gang.
As originally determined in https://twitter.com/kot_2010/status/927119253324619776, building a pre-size-hinted mutable
Vecas the output of a loop (even for very smalln) can occasionally be more than twice as fast ascollecting into aVec. As demonstrated here https://github.com/stuhood/rust-vec-bench, usingextendinto a pre-size-hintedVeccloses most of the gap.Focusing on improving
Iterator::size_hintimplementations so that they suggest better sizes forcollectwould likely help reduce this gap. But there are diminishing returns to heuristics, and in many cases users should be able to give better explicit hints.This issue initially proposed adding an
Iterator::collect_with_size_hintmethod, but @scottmcm 's suggestion to addIterator::collect_into<E: Extend>(e: E) -> Eseems more general, and suggests a useful analogue inunzip_into<E1: Extend, E2: Extend>(e1: E1, e2: E2) -> (E1, E2).