diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 135279874bb3b7b3d7a1e0be730f3c40f82bab6c..69df60a7c3e184f6d7a9e42a4189d5d7d1b593be 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -2712,6 +2712,13 @@ fn as_mut(&mut self) -> &mut [T] { #[stable(feature = "rust1", since = "1.0.0")] impl From<&[T]> for Vec { + /// Allocate a `Vec` and fill it by cloning `s`'s items. + /// + /// # Examples + /// + /// ``` + /// assert_eq!(Vec::from(&[1, 2, 3][..]), vec![1, 2, 3]); + /// ``` #[cfg(not(test))] fn from(s: &[T]) -> Vec { s.to_vec() @@ -2724,6 +2731,13 @@ fn from(s: &[T]) -> Vec { #[stable(feature = "vec_from_mut", since = "1.19.0")] impl From<&mut [T]> for Vec { + /// Allocate a `Vec` and fill it by cloning `s`'s items. + /// + /// # Examples + /// + /// ``` + /// assert_eq!(Vec::from(&mut [1, 2, 3][..]), vec![1, 2, 3]); + /// ``` #[cfg(not(test))] fn from(s: &mut [T]) -> Vec { s.to_vec() @@ -2740,6 +2754,13 @@ fn from(s: &mut [T]) -> Vec { fn from(s: [T; N]) -> Vec { <[T]>::into_vec(box s) } + /// Allocate a `Vec` and move `s`'s items into it. + /// + /// # Examples + /// + /// ``` + /// assert_eq!(Vec::from([1, 2, 3]), vec![1, 2, 3]); + /// ``` #[cfg(test)] fn from(s: [T; N]) -> Vec { crate::slice::into_vec(box s) @@ -2751,6 +2772,20 @@ impl<'a, T> From> for Vec where [T]: ToOwned>, { + /// Convert a clone-on-write slice into a vector. + /// + /// If `s` already owns a `Vec`, it will be returned directly. + /// If `s` is borrowing a slice, a new `Vec` will be allocated and + /// filled by cloning `s`'s items into it. + /// + /// # Examples + /// + /// ``` + /// # use std::borrow::Cow; + /// let o: Cow<[i32]> = Cow::Owned(vec![1, 2, 3]); + /// let b: Cow<[i32]> = Cow::Borrowed(&[1, 2, 3]); + /// assert_eq!(Vec::from(o), Vec::from(b)); + /// ``` fn from(s: Cow<'a, [T]>) -> Vec { s.into_owned() } @@ -2760,6 +2795,15 @@ fn from(s: Cow<'a, [T]>) -> Vec { #[cfg(not(test))] #[stable(feature = "vec_from_box", since = "1.18.0")] impl From> for Vec { + /// Convert a boxed slice into a vector. + /// No heap allocation is performed, and the items are not copied. + /// + /// # Examples + /// + /// ``` + /// let b: Box<[i32]> = vec![1, 2, 3].into_boxed_slice(); + /// assert_eq!(Vec::from(b), vec![1, 2, 3]); + /// ``` fn from(s: Box<[T], A>) -> Self { let len = s.len(); Self { buf: RawVec::from_box(s), len } @@ -2770,6 +2814,16 @@ fn from(s: Box<[T], A>) -> Self { #[cfg(not(test))] #[stable(feature = "box_from_vec", since = "1.20.0")] impl From> for Box<[T], A> { + /// Convert a vector into a boxed slice. + /// + /// If `v` has excess capacity, its items will be moved into a + /// newly-allocated buffer with exactly the right capacity. + /// + /// # Examples + /// + /// ``` + /// assert_eq!(Box::from(vec![1, 2, 3]), vec![1, 2, 3].into_boxed_slice()); + /// ``` fn from(v: Vec) -> Self { v.into_boxed_slice() } @@ -2777,6 +2831,13 @@ fn from(v: Vec) -> Self { #[stable(feature = "rust1", since = "1.0.0")] impl From<&str> for Vec { + /// Allocate a `Vec` and fill it with a UTF-8 string. + /// + /// # Examples + /// + /// ``` + /// assert_eq!(Vec::from("123"), vec![b'1', b'2', b'3']); + /// ``` fn from(s: &str) -> Vec { From::from(s.as_bytes()) }