Skip to content

Commit 58f4c0f

Browse files
authored
Rollup merge of #85715 - fee1-dead:document-string, r=JohnTitor
Document `From` impls in string.rs
2 parents e6763c9 + 25e5a71 commit 58f4c0f

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
@@ -2491,6 +2491,9 @@ impl AsRef<[u8]> for String {
24912491
#[cfg(not(no_global_oom_handling))]
24922492
#[stable(feature = "rust1", since = "1.0.0")]
24932493
impl From<&str> for String {
2494+
/// Converts a `&str` into a [`String`].
2495+
///
2496+
/// The result is allocated on the heap.
24942497
#[inline]
24952498
fn from(s: &str) -> String {
24962499
s.to_owned()
@@ -2500,7 +2503,7 @@ impl From<&str> for String {
25002503
#[cfg(not(no_global_oom_handling))]
25012504
#[stable(feature = "from_mut_str_for_string", since = "1.44.0")]
25022505
impl From<&mut str> for String {
2503-
/// Converts a `&mut str` into a `String`.
2506+
/// Converts a `&mut str` into a [`String`].
25042507
///
25052508
/// The result is allocated on the heap.
25062509
#[inline]
@@ -2512,6 +2515,9 @@ impl From<&mut str> for String {
25122515
#[cfg(not(no_global_oom_handling))]
25132516
#[stable(feature = "from_ref_string", since = "1.35.0")]
25142517
impl From<&String> for String {
2518+
/// Converts a `&String` into a [`String`].
2519+
///
2520+
/// This clones `s` and returns the clone.
25152521
#[inline]
25162522
fn from(s: &String) -> String {
25172523
s.clone()
@@ -2522,7 +2528,7 @@ impl From<&String> for String {
25222528
#[cfg(not(test))]
25232529
#[stable(feature = "string_from_box", since = "1.18.0")]
25242530
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`].
25262532
/// It is notable that the `str` slice is owned.
25272533
///
25282534
/// # Examples
@@ -2544,7 +2550,7 @@ impl From<Box<str>> for String {
25442550
#[cfg(not(no_global_oom_handling))]
25452551
#[stable(feature = "box_from_str", since = "1.20.0")]
25462552
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.
25482554
///
25492555
/// # Examples
25502556
///
@@ -2565,6 +2571,22 @@ impl From<String> for Box<str> {
25652571
#[cfg(not(no_global_oom_handling))]
25662572
#[stable(feature = "string_from_cow_str", since = "1.14.0")]
25672573
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+
/// ```
25682590
fn from(s: Cow<'a, str>) -> String {
25692591
s.into_owned()
25702592
}
@@ -2573,7 +2595,7 @@ impl<'a> From<Cow<'a, str>> for String {
25732595
#[cfg(not(no_global_oom_handling))]
25742596
#[stable(feature = "rust1", since = "1.0.0")]
25752597
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.
25772599
/// No heap allocation is performed, and the string
25782600
/// is not copied.
25792601
///
@@ -2583,6 +2605,8 @@ impl<'a> From<&'a str> for Cow<'a, str> {
25832605
/// # use std::borrow::Cow;
25842606
/// assert_eq!(Cow::from("eggplant"), Cow::Borrowed("eggplant"));
25852607
/// ```
2608+
///
2609+
/// [`Borrowed`]: crate::borrow::Cow::Borrowed
25862610
#[inline]
25872611
fn from(s: &'a str) -> Cow<'a, str> {
25882612
Cow::Borrowed(s)
@@ -2592,7 +2616,7 @@ impl<'a> From<&'a str> for Cow<'a, str> {
25922616
#[cfg(not(no_global_oom_handling))]
25932617
#[stable(feature = "rust1", since = "1.0.0")]
25942618
impl<'a> From<String> for Cow<'a, str> {
2595-
/// Converts a String into an Owned variant.
2619+
/// Converts a [`String`] into an [`Owned`] variant.
25962620
/// No heap allocation is performed, and the string
25972621
/// is not copied.
25982622
///
@@ -2604,6 +2628,8 @@ impl<'a> From<String> for Cow<'a, str> {
26042628
/// let s2 = "eggplant".to_string();
26052629
/// assert_eq!(Cow::from(s), Cow::<'static, str>::Owned(s2));
26062630
/// ```
2631+
///
2632+
/// [`Owned`]: crate::borrow::Cow::Owned
26072633
#[inline]
26082634
fn from(s: String) -> Cow<'a, str> {
26092635
Cow::Owned(s)
@@ -2613,7 +2639,7 @@ impl<'a> From<String> for Cow<'a, str> {
26132639
#[cfg(not(no_global_oom_handling))]
26142640
#[stable(feature = "cow_from_string_ref", since = "1.28.0")]
26152641
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.
26172643
/// No heap allocation is performed, and the string
26182644
/// is not copied.
26192645
///
@@ -2624,6 +2650,8 @@ impl<'a> From<&'a String> for Cow<'a, str> {
26242650
/// let s = "eggplant".to_string();
26252651
/// assert_eq!(Cow::from(&s), Cow::Borrowed("eggplant"));
26262652
/// ```
2653+
///
2654+
/// [`Borrowed`]: crate::borrow::Cow::Borrowed
26272655
#[inline]
26282656
fn from(s: &'a String) -> Cow<'a, str> {
26292657
Cow::Borrowed(s.as_str())
@@ -2656,7 +2684,7 @@ impl<'a> FromIterator<String> for Cow<'a, str> {
26562684

26572685
#[stable(feature = "from_string_for_vec_u8", since = "1.14.0")]
26582686
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`].
26602688
///
26612689
/// # Examples
26622690
///
@@ -2802,6 +2830,14 @@ impl FusedIterator for Drain<'_> {}
28022830
#[cfg(not(no_global_oom_handling))]
28032831
#[stable(feature = "from_char_for_string", since = "1.46.0")]
28042832
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+
/// ```
28052841
#[inline]
28062842
fn from(c: char) -> Self {
28072843
c.to_string()

0 commit comments

Comments
 (0)