Skip to content

Commit

Permalink
Add missing examples for Option type
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Aug 30, 2019
1 parent bbd48e6 commit fdc4f90
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/libcore/option.rs
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 fdc4f90

Please sign in to comment.