Skip to content

Commit a6c1f61

Browse files
authored
Rollup merge of #71909 - Dolpheyn:doc-from-trait-for-option, r=steveklabnik
Document From trait for Option implementations Add documentation for ```From``` trait for ```std::option::Option``` implementations This PR solves a part of #51430 ( CC @skade ) This is my first PR ever in contributing for OSS. I'm happy to learn and make any changes if necessary :)
2 parents 720ec68 + 6c3856f commit a6c1f61

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/libcore/option.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,20 +1357,65 @@ impl<'a, T> IntoIterator for &'a mut Option<T> {
13571357

13581358
#[stable(since = "1.12.0", feature = "option_from")]
13591359
impl<T> From<T> for Option<T> {
1360+
/// Copies `val` into a new `Some`.
1361+
///
1362+
/// # Examples
1363+
///
1364+
/// ```
1365+
/// let o: Option<u8> = Option::from(67);
1366+
///
1367+
/// assert_eq!(Some(67), o);
1368+
/// ```
13601369
fn from(val: T) -> Option<T> {
13611370
Some(val)
13621371
}
13631372
}
13641373

13651374
#[stable(feature = "option_ref_from_ref_option", since = "1.30.0")]
13661375
impl<'a, T> From<&'a Option<T>> for Option<&'a T> {
1376+
/// Converts from `&Option<T>` to `Option<&T>`.
1377+
///
1378+
/// # Examples
1379+
///
1380+
/// Converts an `Option<`[`String`]`>` into an `Option<`[`usize`]`>`, preserving the original.
1381+
/// The [`map`] method takes the `self` argument by value, consuming the original,
1382+
/// so this technique uses `as_ref` to first take an `Option` to a reference
1383+
/// to the value inside the original.
1384+
///
1385+
/// [`map`]: ../../std/option/enum.Option.html#method.map
1386+
/// [`String`]: ../../std/string/struct.String.html
1387+
/// [`usize`]: ../../std/primitive.usize.html
1388+
///
1389+
/// ```
1390+
/// let s: Option<String> = Some(String::from("Hello, Rustaceans!"));
1391+
/// let o: Option<usize> = Option::from(&s).map(|ss: &String| ss.len());
1392+
///
1393+
/// println!("Can still print s: {:?}", s);
1394+
///
1395+
/// assert_eq!(o, Some(18));
1396+
/// ```
13671397
fn from(o: &'a Option<T>) -> Option<&'a T> {
13681398
o.as_ref()
13691399
}
13701400
}
13711401

13721402
#[stable(feature = "option_ref_from_ref_option", since = "1.30.0")]
13731403
impl<'a, T> From<&'a mut Option<T>> for Option<&'a mut T> {
1404+
/// Converts from `&mut Option<T>` to `Option<&mut T>`
1405+
///
1406+
/// # Examples
1407+
///
1408+
/// ```
1409+
/// let mut s = Some(String::from("Hello"));
1410+
/// let o: Option<&mut String> = Option::from(&mut s);
1411+
///
1412+
/// match o {
1413+
/// Some(t) => *t = String::from("Hello, Rustaceans!"),
1414+
/// None => (),
1415+
/// }
1416+
///
1417+
/// assert_eq!(s, Some(String::from("Hello, Rustaceans!")));
1418+
/// ```
13741419
fn from(o: &'a mut Option<T>) -> Option<&'a mut T> {
13751420
o.as_mut()
13761421
}

0 commit comments

Comments
 (0)