@@ -2712,6 +2712,13 @@ impl<T, A: Allocator> AsMut<[T]> for Vec<T, A> {
2712
2712
2713
2713
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2714
2714
impl < 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
+ /// ```
2715
2722
#[ cfg( not( test) ) ]
2716
2723
fn from ( s : & [ T ] ) -> Vec < T > {
2717
2724
s. to_vec ( )
@@ -2724,6 +2731,13 @@ impl<T: Clone> From<&[T]> for Vec<T> {
2724
2731
2725
2732
#[ stable( feature = "vec_from_mut" , since = "1.19.0" ) ]
2726
2733
impl < 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
+ /// ```
2727
2741
#[ cfg( not( test) ) ]
2728
2742
fn from ( s : & mut [ T ] ) -> Vec < T > {
2729
2743
s. to_vec ( )
@@ -2740,6 +2754,13 @@ impl<T, const N: usize> From<[T; N]> for Vec<T> {
2740
2754
fn from ( s : [ T ; N ] ) -> Vec < T > {
2741
2755
<[ T ] >:: into_vec ( box s)
2742
2756
}
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
+ /// ```
2743
2764
#[ cfg( test) ]
2744
2765
fn from ( s : [ T ; N ] ) -> Vec < T > {
2745
2766
crate :: slice:: into_vec ( box s)
@@ -2751,6 +2772,20 @@ impl<'a, T> From<Cow<'a, [T]>> for Vec<T>
2751
2772
where
2752
2773
[ T ] : ToOwned < Owned = Vec < T > > ,
2753
2774
{
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
+ /// ```
2754
2789
fn from ( s : Cow < ' a , [ T ] > ) -> Vec < T > {
2755
2790
s. into_owned ( )
2756
2791
}
@@ -2760,6 +2795,15 @@ where
2760
2795
#[ cfg( not( test) ) ]
2761
2796
#[ stable( feature = "vec_from_box" , since = "1.18.0" ) ]
2762
2797
impl < 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
+ /// ```
2763
2807
fn from ( s : Box < [ T ] , A > ) -> Self {
2764
2808
let len = s. len ( ) ;
2765
2809
Self { buf : RawVec :: from_box ( s) , len }
@@ -2770,13 +2814,30 @@ impl<T, A: Allocator> From<Box<[T], A>> for Vec<T, A> {
2770
2814
#[ cfg( not( test) ) ]
2771
2815
#[ stable( feature = "box_from_vec" , since = "1.20.0" ) ]
2772
2816
impl < 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
+ /// ```
2773
2827
fn from ( v : Vec < T , A > ) -> Self {
2774
2828
v. into_boxed_slice ( )
2775
2829
}
2776
2830
}
2777
2831
2778
2832
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2779
2833
impl 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
+ /// ```
2780
2841
fn from ( s : & str ) -> Vec < u8 > {
2781
2842
From :: from ( s. as_bytes ( ) )
2782
2843
}
0 commit comments