Skip to content

Commit 4b9759e

Browse files
committed
Add a to_owned_vec method to IteratorUtil.
1 parent 89c4af0 commit 4b9759e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/libstd/iterator.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,18 @@ pub trait IteratorUtil<A> {
333333
/// ~~~
334334
fn collect<B: FromIterator<A, Self>>(&mut self) -> B;
335335

336+
/// Loops through the entire iterator, collecting all of the elements into
337+
/// a unique vector. This is simply collect() specialized for vectors.
338+
///
339+
/// # Example
340+
///
341+
/// ~~~ {.rust}
342+
/// let a = [1, 2, 3, 4, 5];
343+
/// let b: ~[int] = a.iter().transform(|&x| x).to_owned_vec();
344+
/// assert!(a == b);
345+
/// ~~~
346+
fn to_owned_vec(&mut self) -> ~[A];
347+
336348
/// Loops through `n` iterations, returning the `n`th element of the
337349
/// iterator.
338350
///
@@ -529,6 +541,11 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
529541
FromIterator::from_iterator(self)
530542
}
531543

544+
#[inline]
545+
fn to_owned_vec(&mut self) -> ~[A] {
546+
self.collect()
547+
}
548+
532549
/// Return the `n`th item yielded by an iterator.
533550
#[inline]
534551
fn nth(&mut self, mut n: uint) -> Option<A> {

0 commit comments

Comments
 (0)