From c83218de12748a678e873813a6b8381f46fae590 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 4 Oct 2012 15:14:28 -0700 Subject: [PATCH] core: Improve option docs a little --- src/libcore/option.rs | 136 ++++++++++++++++++++++++++++++++---------- 1 file changed, 105 insertions(+), 31 deletions(-) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 6bd326186cb..1cdd4511e29 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -1,12 +1,35 @@ /*! - * Operations on the ubiquitous `option` type. - * - * Type `option` represents an optional value. - * - * Every `Option` value can either be `Some(T)` or `none`. Where in other - * languages you might use a nullable type, in Rust you would use an option - * type. - */ + +Operations on the ubiquitous `Option` type. + +Type `Option` represents an optional value. + +Every `Option` value can either be `Some(T)` or `None`. Where in other +languages you might use a nullable type, in Rust you would use an option +type. + +Options are most commonly used with pattern matching to query the presence +of a value and take action, always accounting for the `None` case. + +# Example + +~~~ +let msg = Some(~"howdy"); + +// Take a reference to the contained string +match msg { + Some(ref m) => io::println(m), + None => () +} + +// Remove the contained string, destroying the Option +let unwrapped_msg = match move msg { + Some(move m) => m, + None => ~"default message" +}; +~~~ + +*/ // NB: transitionary, de-mode-ing. #[warn(deprecated_mode)]; @@ -22,12 +45,19 @@ pub enum Option { pub pure fn get(opt: &Option) -> T { /*! - * Gets the value out of an option - * - * # Failure - * - * Fails if the value equals `none` - */ + Gets the value out of an option + + # Failure + + Fails if the value equals `None` + + # Safety note + + In general, because this function may fail, its use is discouraged + (calling `get` on `None` is akin to dereferencing a null pointer). + Instead, prefer to use pattern matching and handle the `None` + case explicitly. + */ match *opt { Some(copy x) => return x, @@ -37,11 +67,18 @@ pub pure fn get(opt: &Option) -> T { pub pure fn get_ref(opt: &r/Option) -> &r/T { /*! - * Gets an immutable reference to the value inside an option. - * - * # Failure - * - * Fails if the value equals `none` + Gets an immutable reference to the value inside an option. + + # Failure + + Fails if the value equals `None` + + # Safety note + + In general, because this function may fail, its use is discouraged + (calling `get` on `None` is akin to dereferencing a null pointer). + Instead, prefer to use pattern matching and handle the `None` + case explicitly. */ match *opt { Some(ref x) => x, @@ -154,10 +191,20 @@ pub pure fn iter(opt: &Option, f: fn(x: &T)) { #[inline(always)] pub pure fn unwrap(opt: Option) -> T { /*! - * Moves a value out of an option type and returns it. - * - * Useful primarily for getting strings, vectors and unique pointers out - * of option types without copying them. + Moves a value out of an option type and returns it. + + Useful primarily for getting strings, vectors and unique pointers out + of option types without copying them. + + # Failure + + Fails if the value equals `None`. + + # Safety note + + In general, because this function may fail, its use is discouraged. + Instead, prefer to use pattern matching and handle the `None` + case explicitly. */ match move opt { Some(move x) => move x, @@ -165,9 +212,16 @@ pub pure fn unwrap(opt: Option) -> T { } } -/// The ubiquitous option dance. #[inline(always)] pub fn swap_unwrap(opt: &mut Option) -> T { + /*! + The option dance. Moves a value out of an option type and returns it, + replacing the original with `None`. + + # Failure + + Fails if the value equals `None`. + */ if opt.is_none() { fail ~"option::swap_unwrap none" } unwrap(util::replace(opt, None)) } @@ -201,18 +255,38 @@ impl &Option { pure fn iter(f: fn(x: &T)) { iter(self, f) } /// Maps a `some` value from one type to another by reference pure fn map(f: fn(x: &T) -> U) -> Option { map(self, f) } - /// Gets an immutable reference to the value inside a `some`. + /** + Gets an immutable reference to the value inside an option. + + # Failure + + Fails if the value equals `None` + + # Safety note + + In general, because this function may fail, its use is discouraged + (calling `get` on `None` is akin to dereferencing a null pointer). + Instead, prefer to use pattern matching and handle the `None` + case explicitly. + */ pure fn get_ref() -> &self/T { get_ref(self) } } impl Option { /** - * Gets the value out of an option - * - * # Failure - * - * Fails if the value equals `none` - */ + Gets the value out of an option + + # Failure + + Fails if the value equals `None` + + # Safety note + + In general, because this function may fail, its use is discouraged + (calling `get` on `None` is akin to dereferencing a null pointer). + Instead, prefer to use pattern matching and handle the `None` + case explicitly. + */ pure fn get() -> T { get(&self) } pure fn get_default(def: T) -> T { get_default(&self, def) } /**