Skip to content

Commit 475aeab

Browse files
Improve code example for Option::unwrap_or_default
1 parent f5193a9 commit 475aeab

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

library/core/src/option.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -834,20 +834,12 @@ impl<T> Option<T> {
834834
///
835835
/// # Examples
836836
///
837-
/// Converts a string to an integer, turning poorly-formed strings
838-
/// into 0 (the default value for integers). [`parse`] converts
839-
/// a string to any other type that implements [`FromStr`], returning
840-
/// [`None`] on error.
841-
///
842837
/// ```
843-
/// let good_year_from_input = "1909";
844-
/// let bad_year_from_input = "190blarg";
845-
/// // Result::ok() converts a Result<T> to an Option<T>
846-
/// let good_year = good_year_from_input.parse().ok().unwrap_or_default();
847-
/// let bad_year = bad_year_from_input.parse().ok().unwrap_or_default();
838+
/// let x: Option<u32> = None;
839+
/// let y: Option<u32> = Some(12);
848840
///
849-
/// assert_eq!(1909, good_year);
850-
/// assert_eq!(0, bad_year);
841+
/// assert_eq!(x.unwrap_or_default(), 0);
842+
/// assert_eq!(y.unwrap_or_default(), 12);
851843
/// ```
852844
///
853845
/// [default value]: Default::default

0 commit comments

Comments
 (0)