@@ -1110,6 +1110,18 @@ impl<T: Deref> Option<T> {
1110
1110
/// to the original one, additionally coercing the contents via [`Deref`].
1111
1111
///
1112
1112
/// [`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
+ /// ```
1113
1125
pub fn as_deref ( & self ) -> Option < & T :: Target > {
1114
1126
self . as_ref ( ) . map ( |t| t. deref ( ) )
1115
1127
}
@@ -1121,6 +1133,18 @@ impl<T: DerefMut> Option<T> {
1121
1133
///
1122
1134
/// Leaves the original `Option` in-place, creating a new one containing a mutable reference to
1123
1135
/// 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
+ /// ```
1124
1148
pub fn as_deref_mut ( & mut self ) -> Option < & mut T :: Target > {
1125
1149
self . as_mut ( ) . map ( |t| t. deref_mut ( ) )
1126
1150
}
@@ -1199,6 +1223,13 @@ impl<T: Clone> Clone for Option<T> {
1199
1223
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1200
1224
impl < T > Default for Option < T > {
1201
1225
/// Returns [`None`][Option::None].
1226
+ ///
1227
+ /// # Examples
1228
+ ///
1229
+ /// ```
1230
+ /// let opt: Option<u32> = Option::default();
1231
+ /// assert!(opt.is_none());
1232
+ /// ```
1202
1233
#[ inline]
1203
1234
fn default ( ) -> Option < T > { None }
1204
1235
}
0 commit comments