diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2019-08-28 13:21:47 +0200 |
|---|---|---|
| committer | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2019-08-30 10:38:11 +0200 |
| commit | fdc4f9028f838605d031248abda0ebfb7450bf9f (patch) | |
| tree | 5dfa90845489bb8be1966bbb11645e5b0906588e /src/libcore | |
| parent | bbd48e6f166739344528a5fff4f02516b77d7916 (diff) | |
| download | rust-fdc4f9028f838605d031248abda0ebfb7450bf9f.tar.gz rust-fdc4f9028f838605d031248abda0ebfb7450bf9f.zip | |
Add missing examples for Option type
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/option.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 259ed36c578..79bd04b7243 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -1110,6 +1110,18 @@ impl<T: Deref> Option<T> { /// to the original one, additionally coercing the contents via [`Deref`]. /// /// [`Deref`]: ../../std/ops/trait.Deref.html + /// + /// # Examples + /// + /// ``` + /// #![feature(inner_deref)] + /// + /// let x: Option<String> = Some("hey".to_owned()); + /// assert_eq!(x.as_deref(), Some("hey")); + /// + /// let x: Option<String> = None; + /// assert_eq!(x.as_deref(), None); + /// ``` pub fn as_deref(&self) -> Option<&T::Target> { self.as_ref().map(|t| t.deref()) } @@ -1121,6 +1133,18 @@ impl<T: DerefMut> Option<T> { /// /// Leaves the original `Option` in-place, creating a new one containing a mutable reference to /// the inner type's `Deref::Target` type. + /// + /// # Examples + /// + /// ``` + /// #![feature(inner_deref)] + /// + /// let mut x: Option<String> = Some("hey".to_owned()); + /// assert_eq!(x.as_deref_mut().map(|x| { + /// x.make_ascii_uppercase(); + /// x + /// }), Some("HEY".to_owned().as_mut_str())); + /// ``` pub fn as_deref_mut(&mut self) -> Option<&mut T::Target> { self.as_mut().map(|t| t.deref_mut()) } @@ -1199,6 +1223,13 @@ impl<T: Clone> Clone for Option<T> { #[stable(feature = "rust1", since = "1.0.0")] impl<T> Default for Option<T> { /// Returns [`None`][Option::None]. + /// + /// # Examples + /// + /// ``` + /// let opt: Option<u32> = Option::default(); + /// assert!(opt.is_none()); + /// ``` #[inline] fn default() -> Option<T> { None } } |
