@@ -2491,6 +2491,9 @@ impl AsRef<[u8]> for String {
2491
2491
#[ cfg( not( no_global_oom_handling) ) ]
2492
2492
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2493
2493
impl From < & str > for String {
2494
+ /// Converts a `&str` into a [`String`].
2495
+ ///
2496
+ /// The result is allocated on the heap.
2494
2497
#[ inline]
2495
2498
fn from ( s : & str ) -> String {
2496
2499
s. to_owned ( )
@@ -2500,7 +2503,7 @@ impl From<&str> for String {
2500
2503
#[ cfg( not( no_global_oom_handling) ) ]
2501
2504
#[ stable( feature = "from_mut_str_for_string" , since = "1.44.0" ) ]
2502
2505
impl From < & mut str > for String {
2503
- /// Converts a `&mut str` into a `String`.
2506
+ /// Converts a `&mut str` into a [ `String`] .
2504
2507
///
2505
2508
/// The result is allocated on the heap.
2506
2509
#[ inline]
@@ -2512,6 +2515,9 @@ impl From<&mut str> for String {
2512
2515
#[ cfg( not( no_global_oom_handling) ) ]
2513
2516
#[ stable( feature = "from_ref_string" , since = "1.35.0" ) ]
2514
2517
impl From < & String > for String {
2518
+ /// Converts a `&String` into a [`String`].
2519
+ ///
2520
+ /// This clones `s` and returns the clone.
2515
2521
#[ inline]
2516
2522
fn from ( s : & String ) -> String {
2517
2523
s. clone ( )
@@ -2522,7 +2528,7 @@ impl From<&String> for String {
2522
2528
#[ cfg( not( test) ) ]
2523
2529
#[ stable( feature = "string_from_box" , since = "1.18.0" ) ]
2524
2530
impl From < Box < str > > for String {
2525
- /// Converts the given boxed `str` slice to a `String`.
2531
+ /// Converts the given boxed `str` slice to a [ `String`] .
2526
2532
/// It is notable that the `str` slice is owned.
2527
2533
///
2528
2534
/// # Examples
@@ -2544,7 +2550,7 @@ impl From<Box<str>> for String {
2544
2550
#[ cfg( not( no_global_oom_handling) ) ]
2545
2551
#[ stable( feature = "box_from_str" , since = "1.20.0" ) ]
2546
2552
impl From < String > for Box < str > {
2547
- /// Converts the given `String` to a boxed `str` slice that is owned.
2553
+ /// Converts the given [ `String`] to a boxed `str` slice that is owned.
2548
2554
///
2549
2555
/// # Examples
2550
2556
///
@@ -2565,6 +2571,22 @@ impl From<String> for Box<str> {
2565
2571
#[ cfg( not( no_global_oom_handling) ) ]
2566
2572
#[ stable( feature = "string_from_cow_str" , since = "1.14.0" ) ]
2567
2573
impl < ' a > From < Cow < ' a , str > > for String {
2574
+ /// Converts a clone-on-write string to an owned
2575
+ /// instance of [`String`].
2576
+ ///
2577
+ /// This extracts the owned string,
2578
+ /// clones the string if it is not already owned.
2579
+ ///
2580
+ /// # Example
2581
+ ///
2582
+ /// ```
2583
+ /// # use std::borrow::Cow;
2584
+ /// // If the string is not owned...
2585
+ /// let cow: Cow<str> = Cow::Borrowed("eggplant");
2586
+ /// // It will allocate on the heap and copy the string.
2587
+ /// let owned: String = String::from(cow);
2588
+ /// assert_eq!(&owned[..], "eggplant");
2589
+ /// ```
2568
2590
fn from ( s : Cow < ' a , str > ) -> String {
2569
2591
s. into_owned ( )
2570
2592
}
@@ -2573,7 +2595,7 @@ impl<'a> From<Cow<'a, str>> for String {
2573
2595
#[ cfg( not( no_global_oom_handling) ) ]
2574
2596
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2575
2597
impl < ' a > From < & ' a str > for Cow < ' a , str > {
2576
- /// Converts a string slice into a Borrowed variant.
2598
+ /// Converts a string slice into a [` Borrowed`] variant.
2577
2599
/// No heap allocation is performed, and the string
2578
2600
/// is not copied.
2579
2601
///
@@ -2583,6 +2605,8 @@ impl<'a> From<&'a str> for Cow<'a, str> {
2583
2605
/// # use std::borrow::Cow;
2584
2606
/// assert_eq!(Cow::from("eggplant"), Cow::Borrowed("eggplant"));
2585
2607
/// ```
2608
+ ///
2609
+ /// [`Borrowed`]: crate::borrow::Cow::Borrowed
2586
2610
#[ inline]
2587
2611
fn from ( s : & ' a str ) -> Cow < ' a , str > {
2588
2612
Cow :: Borrowed ( s)
@@ -2592,7 +2616,7 @@ impl<'a> From<&'a str> for Cow<'a, str> {
2592
2616
#[ cfg( not( no_global_oom_handling) ) ]
2593
2617
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2594
2618
impl < ' a > From < String > for Cow < ' a , str > {
2595
- /// Converts a String into an Owned variant.
2619
+ /// Converts a [` String`] into an [` Owned`] variant.
2596
2620
/// No heap allocation is performed, and the string
2597
2621
/// is not copied.
2598
2622
///
@@ -2604,6 +2628,8 @@ impl<'a> From<String> for Cow<'a, str> {
2604
2628
/// let s2 = "eggplant".to_string();
2605
2629
/// assert_eq!(Cow::from(s), Cow::<'static, str>::Owned(s2));
2606
2630
/// ```
2631
+ ///
2632
+ /// [`Owned`]: crate::borrow::Cow::Owned
2607
2633
#[ inline]
2608
2634
fn from ( s : String ) -> Cow < ' a , str > {
2609
2635
Cow :: Owned ( s)
@@ -2613,7 +2639,7 @@ impl<'a> From<String> for Cow<'a, str> {
2613
2639
#[ cfg( not( no_global_oom_handling) ) ]
2614
2640
#[ stable( feature = "cow_from_string_ref" , since = "1.28.0" ) ]
2615
2641
impl < ' a > From < & ' a String > for Cow < ' a , str > {
2616
- /// Converts a String reference into a Borrowed variant.
2642
+ /// Converts a [` String`] reference into a [` Borrowed`] variant.
2617
2643
/// No heap allocation is performed, and the string
2618
2644
/// is not copied.
2619
2645
///
@@ -2624,6 +2650,8 @@ impl<'a> From<&'a String> for Cow<'a, str> {
2624
2650
/// let s = "eggplant".to_string();
2625
2651
/// assert_eq!(Cow::from(&s), Cow::Borrowed("eggplant"));
2626
2652
/// ```
2653
+ ///
2654
+ /// [`Borrowed`]: crate::borrow::Cow::Borrowed
2627
2655
#[ inline]
2628
2656
fn from ( s : & ' a String ) -> Cow < ' a , str > {
2629
2657
Cow :: Borrowed ( s. as_str ( ) )
@@ -2656,7 +2684,7 @@ impl<'a> FromIterator<String> for Cow<'a, str> {
2656
2684
2657
2685
#[ stable( feature = "from_string_for_vec_u8" , since = "1.14.0" ) ]
2658
2686
impl From < String > for Vec < u8 > {
2659
- /// Converts the given `String` to a vector `Vec` that holds values of type `u8`.
2687
+ /// Converts the given [ `String`] to a vector [ `Vec`] that holds values of type [ `u8`] .
2660
2688
///
2661
2689
/// # Examples
2662
2690
///
@@ -2802,6 +2830,14 @@ impl FusedIterator for Drain<'_> {}
2802
2830
#[ cfg( not( no_global_oom_handling) ) ]
2803
2831
#[ stable( feature = "from_char_for_string" , since = "1.46.0" ) ]
2804
2832
impl From < char > for String {
2833
+ /// Allocates an owned [`String`] from a single character.
2834
+ ///
2835
+ /// # Example
2836
+ /// ```rust
2837
+ /// let c: char = 'a';
2838
+ /// let s: String = String::from(c);
2839
+ /// assert_eq!("a", &s[..]);
2840
+ /// ```
2805
2841
#[ inline]
2806
2842
fn from ( c : char ) -> Self {
2807
2843
c. to_string ( )
0 commit comments