Skip to content

Commit 7b90759

Browse files
committed
Add documentation/example to Extend impl
1 parent 96b7d07 commit 7b90759

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

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

+19-1
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,30 @@ impl Extend<()> for () {
359359
fn extend_one(&mut self, _item: ()) {}
360360
}
361361

362-
#[stable(feature = "extend_for_tuple", since = "1.54.0")]
362+
#[stable(feature = "extend_for_tuple", since = "1.55.0")]
363363
impl<A, B, ExtendA, ExtendB> Extend<(A, B)> for (ExtendA, ExtendB)
364364
where
365365
ExtendA: Extend<A>,
366366
ExtendB: Extend<B>,
367367
{
368+
/// Allows to `extend` a tuple of collections that also implement `Extend`.
369+
///
370+
/// See also: [`Iterator::unzip`]
371+
///
372+
/// # Examples
373+
/// ```
374+
/// let mut tuple = (vec![0], vec![1]);
375+
/// tuple.extend(vec![(2, 3), (4, 5), (6, 7)]);
376+
/// assert_eq!(tuple.0, vec![0, 2, 4, 6]);
377+
/// assert_eq!(tuple.1, vec![1, 3, 5, 7]);
378+
///
379+
/// // also allows for arbitrarily nested tuples
380+
/// let mut nested_tuple = (vec![(1, -1)], vec![(2, -2)]);
381+
/// nested_tuple.extend(vec![((3, -3), (4, -4)), ((5, -5), (6, -6))]);
382+
///
383+
/// assert_eq!(nested_tuple.0, vec![(1, -1), (3, -3), (5, -5)]);
384+
/// assert_eq!(nested_tuple.1, vec![(2, -2), (4, -4), (6, -6)]);
385+
/// ```
368386
fn extend<T: IntoIterator<Item = (A, B)>>(&mut self, into_iter: T) {
369387
let (a, b) = self;
370388
let iter = into_iter.into_iter();

0 commit comments

Comments
 (0)