diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 8bcad62226a0b..cdae4e87d2aab 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -3564,7 +3564,61 @@ where { } +impl Vec { + /// Create a new `Vec` that contains `elem` `n` times. + /// + /// This method requires `T` to implement [`Clone`], + /// in order to be able to clone the passed value. + /// + /// If `n` is zero, the vector will not allocate. + /// + /// # Panics + /// + /// Panics if `size_of::() * n` exceeds `isize::MAX` bytes. + /// + /// # Examples + /// + /// ``` + /// #![feature(vec_from_elem)] + /// let vec = Vec::from_elem(0, 3); + /// assert_eq!(vec, [0; 3]); + /// ``` + #[cfg(not(no_global_oom_handling))] + #[unstable(feature = "vec_from_elem", issue = "164215")] + #[inline] + pub fn from_elem(elem: T, n: usize) -> Self { + ::from_elem(elem, n, Global) + } +} + impl Vec { + /// Create a new `Vec` with the provided allocator that contains `elem` `n` times. + /// + /// This method requires `T` to implement [`Clone`], + /// in order to be able to clone the passed value. + /// + /// If `n` is zero, the vector will not allocate. + /// + /// # Panics + /// + /// Panics if `size_of::() * n` exceeds `isize::MAX` bytes. + /// + /// # Examples + /// + /// ``` + /// #![feature(vec_from_elem)] + /// use std::alloc::System; + /// + /// let vec = Vec::from_elem_in(0, 3, System); + /// assert_eq!(vec, [0; 3]); + /// ``` + #[cfg(not(no_global_oom_handling))] + #[unstable(feature = "vec_from_elem", issue = "164215")] + #[inline] + pub fn from_elem_in(elem: T, n: usize, alloc: A) -> Self { + ::from_elem(elem, n, alloc) + } + /// Resizes the `Vec` in-place so that `len` is equal to `new_len`. /// /// If `new_len` is greater than `len`, the `Vec` is extended by the @@ -3801,14 +3855,7 @@ impl Vec { #[stable(feature = "rust1", since = "1.0.0")] #[rustc_diagnostic_item = "vec_from_elem"] pub fn from_elem(elem: T, n: usize) -> Vec { - ::from_elem(elem, n, Global) -} - -#[doc(hidden)] -#[cfg(not(no_global_oom_handling))] -#[unstable(feature = "allocator_ext", issue = "163177", implied_by = "allocator_api")] -pub fn from_elem_in(elem: T, n: usize, alloc: A) -> Vec { - ::from_elem(elem, n, alloc) + Vec::from_elem(elem, n) } #[cfg(not(no_global_oom_handling))] diff --git a/tests/ui/suggestions/deref-path-method.stderr b/tests/ui/suggestions/deref-path-method.stderr index 8cf8ec9146d74..859e535d22c1e 100644 --- a/tests/ui/suggestions/deref-path-method.stderr +++ b/tests/ui/suggestions/deref-path-method.stderr @@ -9,7 +9,7 @@ note: if you're trying to build a new `Vec<_, _>` consider using one of the foll Vec::::with_capacity Vec::::try_with_capacity Vec::::from_raw_parts - and 7 others + and 9 others --> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL help: the function `contains` is implemented on `[_]` |