diff --git a/src/libcore/option.rs b/src/libcore/option.rs index a71727c3f8e..a4d33ae8028 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -574,13 +574,17 @@ impl ExactSize for Item {} /// Here is an example which increments every integer in a vector, /// checking for overflow: /// -/// fn inc_conditionally(x: uint) -> Option { -/// if x == uint::MAX { return None; } -/// else { return Some(x+1u); } -/// } -/// let v = [1u, 2, 3]; -/// let res = collect(v.iter().map(|&x| inc_conditionally(x))); -/// assert!(res == Some(~[2u, 3, 4])); +/// ```rust +/// use std::option; +/// use std::uint; +/// +/// let v = vec!(1u, 2u); +/// let res: Option> = option::collect(v.iter().map(|x: &uint| +/// if *x == uint::MAX { None } +/// else { Some(x + 1) } +/// )); +/// assert!(res == Some(vec!(2u, 3u))); +/// ``` #[inline] pub fn collect>, V: FromIterator>(iter: Iter) -> Option { // FIXME(#11084): This should be twice as fast once this bug is closed. diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 180addfebcf..3f82190e6b7 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -572,13 +572,17 @@ impl Result { /// Here is an example which increments every integer in a vector, /// checking for overflow: /// -/// fn inc_conditionally(x: uint) -> Result { -/// if x == uint::MAX { return Err("overflow"); } -/// else { return Ok(x+1u); } -/// } -/// let v = [1u, 2, 3]; -/// let res = collect(v.iter().map(|&x| inc_conditionally(x))); -/// assert!(res == Ok(~[2u, 3, 4])); +/// ```rust +/// use std::result; +/// use std::uint; +/// +/// let v = vec!(1u, 2u); +/// let res: Result, &'static str> = result::collect(v.iter().map(|x: &uint| +/// if *x == uint::MAX { Err("Overflow!") } +/// else { Ok(x + 1) } +/// )); +/// assert!(res == Ok(vec!(2u, 3u))); +/// ``` #[inline] pub fn collect>, V: FromIterator>(iter: Iter) -> Result { // FIXME(#11084): This should be twice as fast once this bug is closed.