diff options
| author | Yoshua Wuyts <yoshuawuyts@gmail.com> | 2020-11-20 01:42:43 +0100 |
|---|---|---|
| committer | Yoshua Wuyts <yoshuawuyts@gmail.com> | 2020-11-20 14:12:54 +0100 |
| commit | a64d0d4774db30bd37ec2ede87e966dcdd33dfc2 (patch) | |
| tree | 2673756e7d85bd24862771b77d88a6b6ab99e85c | |
| parent | fe982319aa0aa5bbfc2795791a753832292bd2ba (diff) | |
| download | rust-a64d0d4774db30bd37ec2ede87e966dcdd33dfc2.tar.gz rust-a64d0d4774db30bd37ec2ede87e966dcdd33dfc2.zip | |
Add `core::slice::fill_with`
| -rw-r--r-- | library/core/src/slice/mod.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index 79ae1d5829a..a8f199e17a8 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -2599,6 +2599,34 @@ impl<T> [T] { } } + /// Fills `self` with elements returned by calling a closure repeatedly. + /// + /// This method uses a closure to create new values. If you'd rather + /// [`Clone`] a given value, use [`fill`]. If you want to use the [`Default`] + /// trait to generate values, you can pass [`Default::default`] as the + /// argument. + /// + /// [`fill`]: #method.fill + /// + /// # Examples + /// + /// ``` + /// #![feature(slice_fill_with)] + /// + /// let mut buf = vec![1; 10]; + /// buf.fill_with(Default::default); + /// assert_eq!(buf, vec![0; 10]); + /// ``` + #[unstable(feature = "slice_fill_with", issue = "79221")] + pub fn fill_with<F>(&mut self, mut f: F) + where + F: FnMut() -> T, + { + for el in self { + *el = f(); + } + } + /// Copies the elements from `src` into `self`. /// /// The length of `src` must be the same as `self`. |
