diff options
| author | Yoshua Wuyts <yoshuawuyts@gmail.com> | 2020-04-03 22:53:55 +0200 |
|---|---|---|
| committer | Yoshua Wuyts <yoshuawuyts@gmail.com> | 2020-04-05 01:44:02 +0200 |
| commit | edabceb4a3f0149323c381d5c75fe6957385addf (patch) | |
| tree | fe55ccdb31156f20638959d71caddf3e77267513 /src/libcore/slice | |
| parent | f6fe99c798cb65280a9a56f442b371adcb7b8aa2 (diff) | |
| download | rust-edabceb4a3f0149323c381d5c75fe6957385addf.tar.gz rust-edabceb4a3f0149323c381d5c75fe6957385addf.zip | |
Add slice::fill
Diffstat (limited to 'src/libcore/slice')
| -rw-r--r-- | src/libcore/slice/mod.rs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index 2140a7be9ef..a257d73c0cc 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -23,6 +23,7 @@ // * The `raw` and `bytes` submodules. // * Boilerplate trait implementations. +use crate::borrow::Borrow; use crate::cmp; use crate::cmp::Ordering::{self, Equal, Greater, Less}; use crate::fmt; @@ -2145,6 +2146,29 @@ impl<T> [T] { } } + /// Fills `self` with elements by cloning `value`. + /// + /// # Examples + /// + /// ``` + /// #![feature(slice_fill)] + /// + /// let mut buf = vec![0; 10]; + /// buf.fill(1); + /// assert_eq!(buf, vec![1; 10]); + /// ``` + #[unstable(feature = "slice_fill", issue = "70758")] + pub fn fill<V>(&mut self, value: V) + where + V: Borrow<T>, + T: Clone, + { + let value = value.borrow(); + for el in self { + el.clone_from(value) + } + } + /// Copies the elements from `src` into `self`. /// /// The length of `src` must be the same as `self`. |
