Skip to content

Commit 85e34b2

Browse files
aochagaviaalexcrichton
authored andcommitted
Improved example code in Option
1 parent 437338a commit 85e34b2

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

src/libcore/option.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,23 @@
3030
//! of a value and take action, always accounting for the `None` case.
3131
//!
3232
//! ```
33-
//! # // FIXME This is not the greatest first example
34-
//! // cow_says contains the word "moo"
35-
//! let cow_says = Some("moo");
36-
//! // dog_says does not contain a value
37-
//! let dog_says: Option<&str> = None;
33+
//! fn divide(numerator: f64, denominator: f64) -> Option<f64> {
34+
//! if denominator == 0.0 {
35+
//! None
36+
//! } else {
37+
//! Some(numerator / denominator)
38+
//! }
39+
//! }
40+
//!
41+
//! // The return value of the function is an option
42+
//! let result = divide(2.0, 3.0);
3843
//!
3944
//! // Pattern match to retrieve the value
40-
//! match (cow_says, dog_says) {
41-
//! (Some(cow_words), Some(dog_words)) => {
42-
//! println!("Cow says {} and dog says {}!", cow_words, dog_words);
43-
//! }
44-
//! (Some(cow_words), None) => println!("Cow says {}", cow_words),
45-
//! (None, Some(dog_words)) => println!("Dog says {}", dog_words),
46-
//! (None, None) => println!("Cow and dog are suspiciously silent")
45+
//! match result {
46+
//! // The division was valid
47+
//! Some(x) => println!("Result: {}", x),
48+
//! // The division was invalid
49+
//! None => println!("Cannot divide by 0")
4750
//! }
4851
//! ```
4952
//!

src/libstd/option.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,23 @@
3030
//! of a value and take action, always accounting for the `None` case.
3131
//!
3232
//! ```
33-
//! # // FIXME This is not the greatest first example
34-
//! // cow_says contains the word "moo"
35-
//! let cow_says = Some("moo");
36-
//! // dog_says does not contain a value
37-
//! let dog_says: Option<&str> = None;
33+
//! fn divide(numerator: f64, denominator: f64) -> Option<f64> {
34+
//! if denominator == 0.0 {
35+
//! None
36+
//! } else {
37+
//! Some(numerator / denominator)
38+
//! }
39+
//! }
40+
//!
41+
//! // The return value of the function is an option
42+
//! let result = divide(2.0, 3.0);
3843
//!
3944
//! // Pattern match to retrieve the value
40-
//! match (cow_says, dog_says) {
41-
//! (Some(cow_words), Some(dog_words)) => {
42-
//! println!("Cow says {} and dog says {}!", cow_words, dog_words);
43-
//! }
44-
//! (Some(cow_words), None) => println!("Cow says {}", cow_words),
45-
//! (None, Some(dog_words)) => println!("Dog says {}", dog_words),
46-
//! (None, None) => println!("Cow and dog are suspiciously silent")
45+
//! match result {
46+
//! // The division was valid
47+
//! Some(x) => println!("Result: {}", x),
48+
//! // The division was invalid
49+
//! None => println!("Cannot divide by 0")
4750
//! }
4851
//! ```
4952
//!

0 commit comments

Comments
 (0)