diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-09-06 19:00:43 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-09-06 19:00:43 +0200 |
| commit | b0dfc8a6f1ca2dc10138cede7a5e0300625693fa (patch) | |
| tree | b015ffedc81d40e8c2e1f0796f39e3cc8ac78f49 /src | |
| parent | 4f61eac630822b30b1c2b46f3dd2a9ebe20f7fce (diff) | |
| parent | fdc4f9028f838605d031248abda0ebfb7450bf9f (diff) | |
| download | rust-b0dfc8a6f1ca2dc10138cede7a5e0300625693fa.tar.gz rust-b0dfc8a6f1ca2dc10138cede7a5e0300625693fa.zip | |
Rollup merge of #63969 - GuillaumeGomez:option-docs-example, r=sfackler
Add missing examples for Option type cc @rust-lang/docs
Diffstat (limited to 'src')
| -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 } } |
