Skip to content

Commit 25e5a71

Browse files
committed
Document From impls in string.rs
1 parent 54bdfa1 commit 25e5a71

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

library/alloc/src/string.rs

+43-7
Original file line numberDiff line numberDiff line change
@@ -2438,6 +2438,9 @@ impl AsRef<[u8]> for String {
24382438
#[cfg(not(no_global_oom_handling))]
24392439
#[stable(feature = "rust1", since = "1.0.0")]
24402440
impl From<&str> for String {
2441+
/// Converts a `&str` into a [`String`].
2442+
///
2443+
/// The result is allocated on the heap.
24412444
#[inline]
24422445
fn from(s: &str) -> String {
24432446
s.to_owned()
@@ -2447,7 +2450,7 @@ impl From<&str> for String {
24472450
#[cfg(not(no_global_oom_handling))]
24482451
#[stable(feature = "from_mut_str_for_string", since = "1.44.0")]
24492452
impl From<&mut str> for String {
2450-
/// Converts a `&mut str` into a `String`.
2453+
/// Converts a `&mut str` into a [`String`].
24512454
///
24522455
/// The result is allocated on the heap.
24532456
#[inline]
@@ -2459,6 +2462,9 @@ impl From<&mut str> for String {
24592462
#[cfg(not(no_global_oom_handling))]
24602463
#[stable(feature = "from_ref_string", since = "1.35.0")]
24612464
impl From<&String> for String {
2465+
/// Converts a `&String` into a [`String`].
2466+
///
2467+
/// This clones `s` and returns the clone.
24622468
#[inline]
24632469
fn from(s: &String) -> String {
24642470
s.clone()
@@ -2469,7 +2475,7 @@ impl From<&String> for String {
24692475
#[cfg(not(test))]
24702476
#[stable(feature = "string_from_box", since = "1.18.0")]
24712477
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`].
24732479
/// It is notable that the `str` slice is owned.
24742480
///
24752481
/// # Examples
@@ -2491,7 +2497,7 @@ impl From<Box<str>> for String {
24912497
#[cfg(not(no_global_oom_handling))]
24922498
#[stable(feature = "box_from_str", since = "1.20.0")]
24932499
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.
24952501
///
24962502
/// # Examples
24972503
///
@@ -2512,6 +2518,22 @@ impl From<String> for Box<str> {
25122518
#[cfg(not(no_global_oom_handling))]
25132519
#[stable(feature = "string_from_cow_str", since = "1.14.0")]
25142520
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+
/// ```
25152537
fn from(s: Cow<'a, str>) -> String {
25162538
s.into_owned()
25172539
}
@@ -2520,7 +2542,7 @@ impl<'a> From<Cow<'a, str>> for String {
25202542
#[cfg(not(no_global_oom_handling))]
25212543
#[stable(feature = "rust1", since = "1.0.0")]
25222544
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.
25242546
/// No heap allocation is performed, and the string
25252547
/// is not copied.
25262548
///
@@ -2530,6 +2552,8 @@ impl<'a> From<&'a str> for Cow<'a, str> {
25302552
/// # use std::borrow::Cow;
25312553
/// assert_eq!(Cow::from("eggplant"), Cow::Borrowed("eggplant"));
25322554
/// ```
2555+
///
2556+
/// [`Borrowed`]: crate::borrow::Cow::Borrowed
25332557
#[inline]
25342558
fn from(s: &'a str) -> Cow<'a, str> {
25352559
Cow::Borrowed(s)
@@ -2539,7 +2563,7 @@ impl<'a> From<&'a str> for Cow<'a, str> {
25392563
#[cfg(not(no_global_oom_handling))]
25402564
#[stable(feature = "rust1", since = "1.0.0")]
25412565
impl<'a> From<String> for Cow<'a, str> {
2542-
/// Converts a String into an Owned variant.
2566+
/// Converts a [`String`] into an [`Owned`] variant.
25432567
/// No heap allocation is performed, and the string
25442568
/// is not copied.
25452569
///
@@ -2551,6 +2575,8 @@ impl<'a> From<String> for Cow<'a, str> {
25512575
/// let s2 = "eggplant".to_string();
25522576
/// assert_eq!(Cow::from(s), Cow::<'static, str>::Owned(s2));
25532577
/// ```
2578+
///
2579+
/// [`Owned`]: crate::borrow::Cow::Owned
25542580
#[inline]
25552581
fn from(s: String) -> Cow<'a, str> {
25562582
Cow::Owned(s)
@@ -2560,7 +2586,7 @@ impl<'a> From<String> for Cow<'a, str> {
25602586
#[cfg(not(no_global_oom_handling))]
25612587
#[stable(feature = "cow_from_string_ref", since = "1.28.0")]
25622588
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.
25642590
/// No heap allocation is performed, and the string
25652591
/// is not copied.
25662592
///
@@ -2571,6 +2597,8 @@ impl<'a> From<&'a String> for Cow<'a, str> {
25712597
/// let s = "eggplant".to_string();
25722598
/// assert_eq!(Cow::from(&s), Cow::Borrowed("eggplant"));
25732599
/// ```
2600+
///
2601+
/// [`Borrowed`]: crate::borrow::Cow::Borrowed
25742602
#[inline]
25752603
fn from(s: &'a String) -> Cow<'a, str> {
25762604
Cow::Borrowed(s.as_str())
@@ -2603,7 +2631,7 @@ impl<'a> FromIterator<String> for Cow<'a, str> {
26032631

26042632
#[stable(feature = "from_string_for_vec_u8", since = "1.14.0")]
26052633
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`].
26072635
///
26082636
/// # Examples
26092637
///
@@ -2749,6 +2777,14 @@ impl FusedIterator for Drain<'_> {}
27492777
#[cfg(not(no_global_oom_handling))]
27502778
#[stable(feature = "from_char_for_string", since = "1.46.0")]
27512779
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+
/// ```
27522788
#[inline]
27532789
fn from(c: char) -> Self {
27542790
c.to_string()

0 commit comments

Comments
 (0)