diff options
| author | Adolfo OchagavĂa <aochagavia92@gmail.com> | 2014-05-11 18:23:46 +0200 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2014-05-12 19:52:29 -0700 |
| commit | 85e34b239672a5d8c0d4ef01fae06f6921de9591 (patch) | |
| tree | 1a2e7fac7161c9e3746d782fa05a55fca0f3af91 /src/libstd | |
| parent | 437338ab658d075b50538541541e3049a79f5911 (diff) | |
| download | rust-85e34b239672a5d8c0d4ef01fae06f6921de9591.tar.gz rust-85e34b239672a5d8c0d4ef01fae06f6921de9591.zip | |
Improved example code in Option
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/option.rs | 27 |
1 files changed, 15 insertions, 12 deletions
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<f64> { +//! 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") //! } //! ``` //! |
