Skip to content

Commit

Permalink
Rollup merge of #63969 - GuillaumeGomez:option-docs-example, r=sfackler
Browse files Browse the repository at this point in the history
Add missing examples for Option type

cc @rust-lang/docs
  • Loading branch information
Centril authored Sep 6, 2019
2 parents 4f61eac + fdc4f90 commit b0dfc8a
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand Down Expand Up @@ -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 }
}
Expand Down

0 comments on commit b0dfc8a

Please sign in to comment.