I know of
.extend()
to avoid reallocation during .collect()
let mut new_vec = Vec::with_capacity(size);
new_vec.extend(your_iterator);
I know of combining results of iterator items durring collect
let new: Result<Vec<T>, E> = v.into_iter().collect()
Is there a way to use both concepts at once?
alice
2
fn collect_hint<T, E>(size: usize, iter: impl IntoIterator<Item = Result<T,E>>) -> Result<Vec<T>, E> {
let mut new_vec = Vec::with_capacity(size);
for item in your_iterator {
new_vec.push(item?);
}
Ok(new_vec)
}
2 Likes
excellent so simple in hindsight 
tiny edit: iter
was one placed named your_iterator
fn collect_hint<T, E>(
size: usize,
iter: impl IntoIterator<Item = Result<T, E>>,
) -> Result<Vec<T>, E> {
let mut new_vec = Vec::with_capacity(size);
for item in iter {
new_vec.push(item?);
}
Ok(new_vec)
}
system
Closed
4
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.