Skip to content

Commit fdc4f90

Browse files
Add missing examples for Option type
1 parent bbd48e6 commit fdc4f90

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/libcore/option.rs

+31
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,18 @@ impl<T: Deref> Option<T> {
11101110
/// to the original one, additionally coercing the contents via [`Deref`].
11111111
///
11121112
/// [`Deref`]: ../../std/ops/trait.Deref.html
1113+
///
1114+
/// # Examples
1115+
///
1116+
/// ```
1117+
/// #![feature(inner_deref)]
1118+
///
1119+
/// let x: Option<String> = Some("hey".to_owned());
1120+
/// assert_eq!(x.as_deref(), Some("hey"));
1121+
///
1122+
/// let x: Option<String> = None;
1123+
/// assert_eq!(x.as_deref(), None);
1124+
/// ```
11131125
pub fn as_deref(&self) -> Option<&T::Target> {
11141126
self.as_ref().map(|t| t.deref())
11151127
}
@@ -1121,6 +1133,18 @@ impl<T: DerefMut> Option<T> {
11211133
///
11221134
/// Leaves the original `Option` in-place, creating a new one containing a mutable reference to
11231135
/// the inner type's `Deref::Target` type.
1136+
///
1137+
/// # Examples
1138+
///
1139+
/// ```
1140+
/// #![feature(inner_deref)]
1141+
///
1142+
/// let mut x: Option<String> = Some("hey".to_owned());
1143+
/// assert_eq!(x.as_deref_mut().map(|x| {
1144+
/// x.make_ascii_uppercase();
1145+
/// x
1146+
/// }), Some("HEY".to_owned().as_mut_str()));
1147+
/// ```
11241148
pub fn as_deref_mut(&mut self) -> Option<&mut T::Target> {
11251149
self.as_mut().map(|t| t.deref_mut())
11261150
}
@@ -1199,6 +1223,13 @@ impl<T: Clone> Clone for Option<T> {
11991223
#[stable(feature = "rust1", since = "1.0.0")]
12001224
impl<T> Default for Option<T> {
12011225
/// Returns [`None`][Option::None].
1226+
///
1227+
/// # Examples
1228+
///
1229+
/// ```
1230+
/// let opt: Option<u32> = Option::default();
1231+
/// assert!(opt.is_none());
1232+
/// ```
12021233
#[inline]
12031234
fn default() -> Option<T> { None }
12041235
}

0 commit comments

Comments
 (0)