@@ -2712,6 +2712,13 @@ impl<T, A: Allocator> AsMut<[T]> for Vec<T, A> {
27122712
27132713#[ stable( feature = "rust1" , since = "1.0.0" ) ]
27142714impl < T : Clone > From < & [ T ] > for Vec < T > {
2715+ /// Allocate a `Vec<T>` and fill it by cloning `s`'s items.
2716+ ///
2717+ /// # Examples
2718+ ///
2719+ /// ```
2720+ /// assert_eq!(Vec::from(&[1, 2, 3][..]), vec![1, 2, 3]);
2721+ /// ```
27152722 #[ cfg( not( test) ) ]
27162723 fn from ( s : & [ T ] ) -> Vec < T > {
27172724 s. to_vec ( )
@@ -2724,6 +2731,13 @@ impl<T: Clone> From<&[T]> for Vec<T> {
27242731
27252732#[ stable( feature = "vec_from_mut" , since = "1.19.0" ) ]
27262733impl < T : Clone > From < & mut [ T ] > for Vec < T > {
2734+ /// Allocate a `Vec<T>` and fill it by cloning `s`'s items.
2735+ ///
2736+ /// # Examples
2737+ ///
2738+ /// ```
2739+ /// assert_eq!(Vec::from(&mut [1, 2, 3][..]), vec![1, 2, 3]);
2740+ /// ```
27272741 #[ cfg( not( test) ) ]
27282742 fn from ( s : & mut [ T ] ) -> Vec < T > {
27292743 s. to_vec ( )
@@ -2740,6 +2754,13 @@ impl<T, const N: usize> From<[T; N]> for Vec<T> {
27402754 fn from ( s : [ T ; N ] ) -> Vec < T > {
27412755 <[ T ] >:: into_vec ( box s)
27422756 }
2757+ /// Allocate a `Vec<T>` and move `s`'s items into it.
2758+ ///
2759+ /// # Examples
2760+ ///
2761+ /// ```
2762+ /// assert_eq!(Vec::from([1, 2, 3]), vec![1, 2, 3]);
2763+ /// ```
27432764 #[ cfg( test) ]
27442765 fn from ( s : [ T ; N ] ) -> Vec < T > {
27452766 crate :: slice:: into_vec ( box s)
@@ -2751,6 +2772,20 @@ impl<'a, T> From<Cow<'a, [T]>> for Vec<T>
27512772where
27522773 [ T ] : ToOwned < Owned = Vec < T > > ,
27532774{
2775+ /// Convert a clone-on-write slice into a vector.
2776+ ///
2777+ /// If `s` already owns a `Vec<T>`, it will be returned directly.
2778+ /// If `s` is borrowing a slice, a new `Vec<T>` will be allocated and
2779+ /// filled by cloning `s`'s items into it.
2780+ ///
2781+ /// # Examples
2782+ ///
2783+ /// ```
2784+ /// # use std::borrow::Cow;
2785+ /// let o: Cow<[i32]> = Cow::Owned(vec![1, 2, 3]);
2786+ /// let b: Cow<[i32]> = Cow::Borrowed(&[1, 2, 3]);
2787+ /// assert_eq!(Vec::from(o), Vec::from(b));
2788+ /// ```
27542789 fn from ( s : Cow < ' a , [ T ] > ) -> Vec < T > {
27552790 s. into_owned ( )
27562791 }
@@ -2760,6 +2795,15 @@ where
27602795#[ cfg( not( test) ) ]
27612796#[ stable( feature = "vec_from_box" , since = "1.18.0" ) ]
27622797impl < T , A : Allocator > From < Box < [ T ] , A > > for Vec < T , A > {
2798+ /// Convert a boxed slice into a vector by transferring ownership of
2799+ /// the existing heap allocation.
2800+ ///
2801+ /// # Examples
2802+ ///
2803+ /// ```
2804+ /// let b: Box<[i32]> = vec![1, 2, 3].into_boxed_slice();
2805+ /// assert_eq!(Vec::from(b), vec![1, 2, 3]);
2806+ /// ```
27632807 fn from ( s : Box < [ T ] , A > ) -> Self {
27642808 let len = s. len ( ) ;
27652809 Self { buf : RawVec :: from_box ( s) , len }
@@ -2770,13 +2814,30 @@ impl<T, A: Allocator> From<Box<[T], A>> for Vec<T, A> {
27702814#[ cfg( not( test) ) ]
27712815#[ stable( feature = "box_from_vec" , since = "1.20.0" ) ]
27722816impl < T , A : Allocator > From < Vec < T , A > > for Box < [ T ] , A > {
2817+ /// Convert a vector into a boxed slice.
2818+ ///
2819+ /// If `v` has excess capacity, its items will be moved into a
2820+ /// newly-allocated buffer with exactly the right capacity.
2821+ ///
2822+ /// # Examples
2823+ ///
2824+ /// ```
2825+ /// assert_eq!(Box::from(vec![1, 2, 3]), vec![1, 2, 3].into_boxed_slice());
2826+ /// ```
27732827 fn from ( v : Vec < T , A > ) -> Self {
27742828 v. into_boxed_slice ( )
27752829 }
27762830}
27772831
27782832#[ stable( feature = "rust1" , since = "1.0.0" ) ]
27792833impl From < & str > for Vec < u8 > {
2834+ /// Allocate a `Vec<u8>` and fill it with a UTF-8 string.
2835+ ///
2836+ /// # Examples
2837+ ///
2838+ /// ```
2839+ /// assert_eq!(Vec::from("123"), vec![b'1', b'2', b'3']);
2840+ /// ```
27802841 fn from ( s : & str ) -> Vec < u8 > {
27812842 From :: from ( s. as_bytes ( ) )
27822843 }
0 commit comments