Skip to content

Commit d8d2e82

Browse files
committed
feat(lib:core:iter): add default impl for type with Default and Extend trait
Signed-off-by: HernandoR <[email protected]>
1 parent 2801f9a commit d8d2e82

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

library/core/src/iter/traits/collect.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ pub trait FromIterator<A>: Sized {
152152
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self;
153153
}
154154

155+
// implement `FromIterator` for type with Default and Extend
156+
impl<T, A: Default + Extend<T>> FromIterator<T> for A {
157+
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
158+
let mut collection = A::default();
159+
collection.extend(iter);
160+
collection
161+
}
162+
}
163+
155164
/// Conversion into an [`Iterator`].
156165
///
157166
/// By implementing `IntoIterator` for a type, you define how it will be

0 commit comments

Comments
 (0)