Skip to content

Commit 5134d9c

Browse files
committed
Rollup merge of #55874 - denisvasilik:docs, r=alexcrichton
string: Add documentation for `From` impls Hi this is part of #51430. I'm a first time contributor, so I started with a small task adding a bit of documentation for From impls.
2 parents 5ccc76f + 6f3add3 commit 5134d9c

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/liballoc/string.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,13 +2206,40 @@ impl<'a> From<&'a str> for String {
22062206
#[cfg(not(test))]
22072207
#[stable(feature = "string_from_box", since = "1.18.0")]
22082208
impl From<Box<str>> for String {
2209+
/// Converts the given boxed `str` slice to a `String`.
2210+
/// It is notable that the `str` slice is owned.
2211+
///
2212+
/// # Examples
2213+
///
2214+
/// Basic usage:
2215+
///
2216+
/// ```
2217+
/// let s1: String = String::from("hello world");
2218+
/// let s2: Box<str> = s1.into_boxed_str();
2219+
/// let s3: String = String::from(s2);
2220+
///
2221+
/// assert_eq!("hello world", s3)
2222+
/// ```
22092223
fn from(s: Box<str>) -> String {
22102224
s.into_string()
22112225
}
22122226
}
22132227

22142228
#[stable(feature = "box_from_str", since = "1.20.0")]
22152229
impl From<String> for Box<str> {
2230+
/// Converts the given `String` to a boxed `str` slice that is owned.
2231+
///
2232+
/// # Examples
2233+
///
2234+
/// Basic usage:
2235+
///
2236+
/// ```
2237+
/// let s1: String = String::from("hello world");
2238+
/// let s2: Box<str> = Box::from(s1);
2239+
/// let s3: String = String::from(s2);
2240+
///
2241+
/// assert_eq!("hello world", s3)
2242+
/// ```
22162243
fn from(s: String) -> Box<str> {
22172244
s.into_boxed_str()
22182245
}
@@ -2272,6 +2299,20 @@ impl<'a> FromIterator<String> for Cow<'a, str> {
22722299

22732300
#[stable(feature = "from_string_for_vec_u8", since = "1.14.0")]
22742301
impl From<String> for Vec<u8> {
2302+
/// Converts the given `String` to a vector `Vec` that holds values of type `u8`.
2303+
///
2304+
/// # Examples
2305+
///
2306+
/// Basic usage:
2307+
///
2308+
/// ```
2309+
/// let s1 = String::from("hello world");
2310+
/// let v1 = Vec::from(s1);
2311+
///
2312+
/// for b in v1 {
2313+
/// println!("{}", b);
2314+
/// }
2315+
/// ```
22752316
fn from(string: String) -> Vec<u8> {
22762317
string.into_bytes()
22772318
}

0 commit comments

Comments
 (0)