Skip to content

Commit edabceb

Browse files
committed
Add slice::fill
1 parent f6fe99c commit edabceb

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/libcore/slice/mod.rs

+24
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
// * The `raw` and `bytes` submodules.
2424
// * Boilerplate trait implementations.
2525

26+
use crate::borrow::Borrow;
2627
use crate::cmp;
2728
use crate::cmp::Ordering::{self, Equal, Greater, Less};
2829
use crate::fmt;
@@ -2145,6 +2146,29 @@ impl<T> [T] {
21452146
}
21462147
}
21472148

2149+
/// Fills `self` with elements by cloning `value`.
2150+
///
2151+
/// # Examples
2152+
///
2153+
/// ```
2154+
/// #![feature(slice_fill)]
2155+
///
2156+
/// let mut buf = vec![0; 10];
2157+
/// buf.fill(1);
2158+
/// assert_eq!(buf, vec![1; 10]);
2159+
/// ```
2160+
#[unstable(feature = "slice_fill", issue = "70758")]
2161+
pub fn fill<V>(&mut self, value: V)
2162+
where
2163+
V: Borrow<T>,
2164+
T: Clone,
2165+
{
2166+
let value = value.borrow();
2167+
for el in self {
2168+
el.clone_from(value)
2169+
}
2170+
}
2171+
21482172
/// Copies the elements from `src` into `self`.
21492173
///
21502174
/// The length of `src` must be the same as `self`.

0 commit comments

Comments
 (0)