From 85e34b239672a5d8c0d4ef01fae06f6921de9591 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20Ochagav=C3=ADa?= Date: Sun, 11 May 2014 18:23:46 +0200 Subject: [PATCH] Improved example code in Option --- src/libcore/option.rs | 27 +++++++++++++++------------ src/libstd/option.rs | 27 +++++++++++++++------------ 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index fd6d174a703..886b7315152 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -30,20 +30,23 @@ //! of a value and take action, always accounting for the `None` case. //! //! ``` -//! # // FIXME This is not the greatest first example -//! // cow_says contains the word "moo" -//! let cow_says = Some("moo"); -//! // dog_says does not contain a value -//! let dog_says: Option<&str> = None; +//! fn divide(numerator: f64, denominator: f64) -> Option { +//! if denominator == 0.0 { +//! None +//! } else { +//! Some(numerator / denominator) +//! } +//! } +//! +//! // The return value of the function is an option +//! let result = divide(2.0, 3.0); //! //! // Pattern match to retrieve the value -//! match (cow_says, dog_says) { -//! (Some(cow_words), Some(dog_words)) => { -//! println!("Cow says {} and dog says {}!", cow_words, dog_words); -//! } -//! (Some(cow_words), None) => println!("Cow says {}", cow_words), -//! (None, Some(dog_words)) => println!("Dog says {}", dog_words), -//! (None, None) => println!("Cow and dog are suspiciously silent") +//! match result { +//! // The division was valid +//! Some(x) => println!("Result: {}", x), +//! // The division was invalid +//! None => println!("Cannot divide by 0") //! } //! ``` //! diff --git a/src/libstd/option.rs b/src/libstd/option.rs index 8fbcd529b63..ad834f2b4d4 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -30,20 +30,23 @@ //! of a value and take action, always accounting for the `None` case. //! //! ``` -//! # // FIXME This is not the greatest first example -//! // cow_says contains the word "moo" -//! let cow_says = Some("moo"); -//! // dog_says does not contain a value -//! let dog_says: Option<&str> = None; +//! fn divide(numerator: f64, denominator: f64) -> Option { +//! if denominator == 0.0 { +//! None +//! } else { +//! Some(numerator / denominator) +//! } +//! } +//! +//! // The return value of the function is an option +//! let result = divide(2.0, 3.0); //! //! // Pattern match to retrieve the value -//! match (cow_says, dog_says) { -//! (Some(cow_words), Some(dog_words)) => { -//! println!("Cow says {} and dog says {}!", cow_words, dog_words); -//! } -//! (Some(cow_words), None) => println!("Cow says {}", cow_words), -//! (None, Some(dog_words)) => println!("Dog says {}", dog_words), -//! (None, None) => println!("Cow and dog are suspiciously silent") +//! match result { +//! // The division was valid +//! Some(x) => println!("Result: {}", x), +//! // The division was invalid +//! None => println!("Cannot divide by 0") //! } //! ``` //!