@@ -2438,6 +2438,9 @@ impl AsRef<[u8]> for String {
2438
2438
#[ cfg( not( no_global_oom_handling) ) ]
2439
2439
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2440
2440
impl From < & str > for String {
2441
+ /// Converts a `&str` into a [`String`].
2442
+ ///
2443
+ /// The result is allocated on the heap.
2441
2444
#[ inline]
2442
2445
fn from ( s : & str ) -> String {
2443
2446
s. to_owned ( )
@@ -2447,7 +2450,7 @@ impl From<&str> for String {
2447
2450
#[ cfg( not( no_global_oom_handling) ) ]
2448
2451
#[ stable( feature = "from_mut_str_for_string" , since = "1.44.0" ) ]
2449
2452
impl From < & mut str > for String {
2450
- /// Converts a `&mut str` into a `String`.
2453
+ /// Converts a `&mut str` into a [ `String`] .
2451
2454
///
2452
2455
/// The result is allocated on the heap.
2453
2456
#[ inline]
@@ -2459,6 +2462,9 @@ impl From<&mut str> for String {
2459
2462
#[ cfg( not( no_global_oom_handling) ) ]
2460
2463
#[ stable( feature = "from_ref_string" , since = "1.35.0" ) ]
2461
2464
impl From < & String > for String {
2465
+ /// Converts a `&String` into a [`String`].
2466
+ ///
2467
+ /// This clones `s` and returns the clone.
2462
2468
#[ inline]
2463
2469
fn from ( s : & String ) -> String {
2464
2470
s. clone ( )
@@ -2469,7 +2475,7 @@ impl From<&String> for String {
2469
2475
#[ cfg( not( test) ) ]
2470
2476
#[ stable( feature = "string_from_box" , since = "1.18.0" ) ]
2471
2477
impl From < Box < str > > for String {
2472
- /// Converts the given boxed `str` slice to a `String`.
2478
+ /// Converts the given boxed `str` slice to a [ `String`] .
2473
2479
/// It is notable that the `str` slice is owned.
2474
2480
///
2475
2481
/// # Examples
@@ -2491,7 +2497,7 @@ impl From<Box<str>> for String {
2491
2497
#[ cfg( not( no_global_oom_handling) ) ]
2492
2498
#[ stable( feature = "box_from_str" , since = "1.20.0" ) ]
2493
2499
impl From < String > for Box < str > {
2494
- /// Converts the given `String` to a boxed `str` slice that is owned.
2500
+ /// Converts the given [ `String`] to a boxed `str` slice that is owned.
2495
2501
///
2496
2502
/// # Examples
2497
2503
///
@@ -2512,6 +2518,22 @@ impl From<String> for Box<str> {
2512
2518
#[ cfg( not( no_global_oom_handling) ) ]
2513
2519
#[ stable( feature = "string_from_cow_str" , since = "1.14.0" ) ]
2514
2520
impl < ' a > From < Cow < ' a , str > > for String {
2521
+ /// Converts a clone-on-write string to an owned
2522
+ /// instance of [`String`].
2523
+ ///
2524
+ /// This extracts the owned string,
2525
+ /// clones the string if it is not already owned.
2526
+ ///
2527
+ /// # Example
2528
+ ///
2529
+ /// ```
2530
+ /// # use std::borrow::Cow;
2531
+ /// // If the string is not owned...
2532
+ /// let cow: Cow<str> = Cow::Borrowed("eggplant");
2533
+ /// // It will allocate on the heap and copy the string.
2534
+ /// let owned: String = String::from(cow);
2535
+ /// assert_eq!(&owned[..], "eggplant");
2536
+ /// ```
2515
2537
fn from ( s : Cow < ' a , str > ) -> String {
2516
2538
s. into_owned ( )
2517
2539
}
@@ -2520,7 +2542,7 @@ impl<'a> From<Cow<'a, str>> for String {
2520
2542
#[ cfg( not( no_global_oom_handling) ) ]
2521
2543
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2522
2544
impl < ' a > From < & ' a str > for Cow < ' a , str > {
2523
- /// Converts a string slice into a Borrowed variant.
2545
+ /// Converts a string slice into a [` Borrowed`] variant.
2524
2546
/// No heap allocation is performed, and the string
2525
2547
/// is not copied.
2526
2548
///
@@ -2530,6 +2552,8 @@ impl<'a> From<&'a str> for Cow<'a, str> {
2530
2552
/// # use std::borrow::Cow;
2531
2553
/// assert_eq!(Cow::from("eggplant"), Cow::Borrowed("eggplant"));
2532
2554
/// ```
2555
+ ///
2556
+ /// [`Borrowed`]: crate::borrow::Cow::Borrowed
2533
2557
#[ inline]
2534
2558
fn from ( s : & ' a str ) -> Cow < ' a , str > {
2535
2559
Cow :: Borrowed ( s)
@@ -2539,7 +2563,7 @@ impl<'a> From<&'a str> for Cow<'a, str> {
2539
2563
#[ cfg( not( no_global_oom_handling) ) ]
2540
2564
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2541
2565
impl < ' a > From < String > for Cow < ' a , str > {
2542
- /// Converts a String into an Owned variant.
2566
+ /// Converts a [` String`] into an [` Owned`] variant.
2543
2567
/// No heap allocation is performed, and the string
2544
2568
/// is not copied.
2545
2569
///
@@ -2551,6 +2575,8 @@ impl<'a> From<String> for Cow<'a, str> {
2551
2575
/// let s2 = "eggplant".to_string();
2552
2576
/// assert_eq!(Cow::from(s), Cow::<'static, str>::Owned(s2));
2553
2577
/// ```
2578
+ ///
2579
+ /// [`Owned`]: crate::borrow::Cow::Owned
2554
2580
#[ inline]
2555
2581
fn from ( s : String ) -> Cow < ' a , str > {
2556
2582
Cow :: Owned ( s)
@@ -2560,7 +2586,7 @@ impl<'a> From<String> for Cow<'a, str> {
2560
2586
#[ cfg( not( no_global_oom_handling) ) ]
2561
2587
#[ stable( feature = "cow_from_string_ref" , since = "1.28.0" ) ]
2562
2588
impl < ' a > From < & ' a String > for Cow < ' a , str > {
2563
- /// Converts a String reference into a Borrowed variant.
2589
+ /// Converts a [` String`] reference into a [` Borrowed`] variant.
2564
2590
/// No heap allocation is performed, and the string
2565
2591
/// is not copied.
2566
2592
///
@@ -2571,6 +2597,8 @@ impl<'a> From<&'a String> for Cow<'a, str> {
2571
2597
/// let s = "eggplant".to_string();
2572
2598
/// assert_eq!(Cow::from(&s), Cow::Borrowed("eggplant"));
2573
2599
/// ```
2600
+ ///
2601
+ /// [`Borrowed`]: crate::borrow::Cow::Borrowed
2574
2602
#[ inline]
2575
2603
fn from ( s : & ' a String ) -> Cow < ' a , str > {
2576
2604
Cow :: Borrowed ( s. as_str ( ) )
@@ -2603,7 +2631,7 @@ impl<'a> FromIterator<String> for Cow<'a, str> {
2603
2631
2604
2632
#[ stable( feature = "from_string_for_vec_u8" , since = "1.14.0" ) ]
2605
2633
impl From < String > for Vec < u8 > {
2606
- /// Converts the given `String` to a vector `Vec` that holds values of type `u8`.
2634
+ /// Converts the given [ `String`] to a vector [ `Vec`] that holds values of type [ `u8`] .
2607
2635
///
2608
2636
/// # Examples
2609
2637
///
@@ -2749,6 +2777,14 @@ impl FusedIterator for Drain<'_> {}
2749
2777
#[ cfg( not( no_global_oom_handling) ) ]
2750
2778
#[ stable( feature = "from_char_for_string" , since = "1.46.0" ) ]
2751
2779
impl From < char > for String {
2780
+ /// Allocates an owned [`String`] from a single character.
2781
+ ///
2782
+ /// # Example
2783
+ /// ```rust
2784
+ /// let c: char = 'a';
2785
+ /// let s: String = String::from(c);
2786
+ /// assert_eq!("a", &s[..]);
2787
+ /// ```
2752
2788
#[ inline]
2753
2789
fn from ( c : char ) -> Self {
2754
2790
c. to_string ( )
0 commit comments