Skip to content

Commit 8182680

Browse files
committed
pub fn from_elem(elem: A::Item, n: usize) -> Self where A::Item: Clone
1 parent 4749fc2 commit 8182680

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,19 @@ impl<A: Array> SmallVec<A> where A::Item: Clone {
747747
self.truncate(len);
748748
}
749749
}
750+
751+
/// Creates a `SmallVec` with `n` copies of `elem`.
752+
/// ```
753+
/// use smallvec::SmallVec;
754+
///
755+
/// let v = SmallVec::<[char; 128]>::from_elem('d', 2);
756+
/// assert_eq!(v, SmallVec::from_buf(['d', 'd']));
757+
/// ```
758+
pub fn from_elem(elem: A::Item, n: usize) -> Self {
759+
let mut v = SmallVec::with_capacity(n);
760+
v.insert_many(0, (0..n).map(|_| elem.clone()));
761+
v
762+
}
750763
}
751764

752765
impl<A: Array> ops::Deref for SmallVec<A> {

0 commit comments

Comments
 (0)