@@ -311,14 +311,13 @@ impl<T, E> Result<T, E> {
311311 ///
312312 /// # Example
313313 ///
314- /// ~~~
315- /// use std::io::{File, Open, Write};
316- ///
317- /// # fn do_not_run_example() { // creates a file
318- /// let mut file = File::open_mode(&Path::new("secret.txt"), Open, Write);
319- /// assert!(file.write_line("it's cold in here").is_ok());
320- /// # }
321- /// ~~~
314+ /// ```
315+ /// let x: Result<int, &str> = Ok(-3);
316+ /// assert_eq!(x.is_ok(), true);
317+ ///
318+ /// let x: Result<int, &str> = Err("Some error message");
319+ /// assert_eq!(x.is_ok(), false);
320+ /// ```
322321 #[ inline]
323322 #[ stable]
324323 pub fn is_ok ( & self ) -> bool {
@@ -332,14 +331,13 @@ impl<T, E> Result<T, E> {
332331 ///
333332 /// # Example
334333 ///
335- /// ~~~
336- /// use std::io::{File, Open, Read};
334+ /// ```
335+ /// let x: Result<int, &str> = Ok(-3);
336+ /// assert_eq!(x.is_err(), false);
337337 ///
338- /// // When opening with `Read` access, if the file does not exist
339- /// // then `open_mode` returns an error.
340- /// let bogus = File::open_mode(&Path::new("not_a_file.txt"), Open, Read);
341- /// assert!(bogus.is_err());
342- /// ~~~
338+ /// let x: Result<int, &str> = Err("Some error message");
339+ /// assert_eq!(x.is_err(), true);
340+ /// ```
343341 #[ inline]
344342 #[ stable]
345343 pub fn is_err ( & self ) -> bool {
@@ -356,18 +354,15 @@ impl<T, E> Result<T, E> {
356354 /// Converts `self` into an `Option<T>`, consuming `self`,
357355 /// and discarding the error, if any.
358356 ///
359- /// To convert to an `Option` without discarding the error value,
360- /// use `as_ref` to first convert the `Result<T, E>` into a
361- /// `Result<&T, &E>`.
362- ///
363- /// # Examples
357+ /// # Example
364358 ///
365- /// ~~~{.should_fail}
366- /// use std::io::{File, IoResult};
359+ /// ```
360+ /// let x: Result<uint, &str> = Ok(2);
361+ /// assert_eq!(x.ok(), Some(2));
367362 ///
368- /// let bdays: IoResult<File > = File::open(&Path::new("important_birthdays.txt") );
369- /// let bdays: File = bdays .ok().expect("unable to open birthday file" );
370- /// ~~~
363+ /// let x: Result<uint, &str > = Err("Nothing here" );
364+ /// assert_eq!(x .ok(), None );
365+ /// ```
371366 #[ inline]
372367 #[ stable]
373368 pub fn ok ( self ) -> Option < T > {
@@ -381,6 +376,16 @@ impl<T, E> Result<T, E> {
381376 ///
382377 /// Converts `self` into an `Option<T>`, consuming `self`,
383378 /// and discarding the value, if any.
379+ ///
380+ /// # Example
381+ ///
382+ /// ```
383+ /// let x: Result<uint, &str> = Ok(2);
384+ /// assert_eq!(x.err(), None);
385+ ///
386+ /// let x: Result<uint, &str> = Err("Nothing here");
387+ /// assert_eq!(x.err(), Some("Nothing here"));
388+ /// ```
384389 #[ inline]
385390 #[ stable]
386391 pub fn err ( self ) -> Option < E > {
@@ -398,6 +403,14 @@ impl<T, E> Result<T, E> {
398403 ///
399404 /// Produces a new `Result`, containing a reference
400405 /// into the original, leaving the original in place.
406+ ///
407+ /// ```
408+ /// let x: Result<uint, &str> = Ok(2);
409+ /// assert_eq!(x.as_ref(), Ok(&2));
410+ ///
411+ /// let x: Result<uint, &str> = Err("Error");
412+ /// assert_eq!(x.as_ref(), Err(&"Error"));
413+ /// ```
401414 #[ inline]
402415 #[ stable]
403416 pub fn as_ref < ' r > ( & ' r self ) -> Result < & ' r T , & ' r E > {
@@ -408,6 +421,23 @@ impl<T, E> Result<T, E> {
408421 }
409422
410423 /// Convert from `Result<T, E>` to `Result<&mut T, &mut E>`
424+ ///
425+ /// ```
426+ /// fn mutate(r: &mut Result<int, int>) {
427+ /// match r.as_mut() {
428+ /// Ok(&ref mut v) => *v = 42,
429+ /// Err(&ref mut e) => *e = 0,
430+ /// }
431+ /// }
432+ ///
433+ /// let mut x: Result<int, int> = Ok(2);
434+ /// mutate(&mut x);
435+ /// assert_eq!(x.unwrap(), 42);
436+ ///
437+ /// let mut x: Result<int, int> = Err(13);
438+ /// mutate(&mut x);
439+ /// assert_eq!(x.unwrap_err(), 0);
440+ /// ```
411441 #[ inline]
412442 #[ unstable = "waiting for mut conventions" ]
413443 pub fn as_mut < ' r > ( & ' r mut self ) -> Result < & ' r mut T , & ' r mut E > {
@@ -418,6 +448,20 @@ impl<T, E> Result<T, E> {
418448 }
419449
420450 /// Convert from `Result<T, E>` to `&mut [T]` (without copying)
451+ ///
452+ /// ```
453+ /// let mut x: Result<&str, uint> = Ok("Gold");
454+ /// {
455+ /// let v = x.as_mut_slice();
456+ /// assert!(v == ["Gold"]);
457+ /// v[0] = "Silver";
458+ /// assert!(v == ["Silver"]);
459+ /// }
460+ /// assert_eq!(x, Ok("Silver"));
461+ ///
462+ /// let mut x: Result<&str, uint> = Err(45);
463+ /// assert!(x.as_mut_slice() == []);
464+ /// ```
421465 #[ inline]
422466 #[ unstable = "waiting for mut conventions" ]
423467 pub fn as_mut_slice < ' r > ( & ' r mut self ) -> & ' r mut [ T ] {
@@ -479,6 +523,18 @@ impl<T, E> Result<T, E> {
479523 ///
480524 /// This function can be used to pass through a successful result while handling
481525 /// an error.
526+ ///
527+ /// # Example
528+ ///
529+ /// ```
530+ /// fn stringify(x: uint) -> String { format!("error code: {}", x) }
531+ ///
532+ /// let x: Result<uint, uint> = Ok(2u);
533+ /// assert_eq!(x.map_err(stringify), Ok(2u));
534+ ///
535+ /// let x: Result<uint, uint> = Err(13);
536+ /// assert_eq!(x.map_err(stringify), Err("error code: 13".to_string()));
537+ /// ```
482538 #[ inline]
483539 #[ unstable = "waiting for unboxed closures" ]
484540 pub fn map_err < F > ( self , op: |E | -> F ) -> Result < T , F > {
@@ -494,6 +550,16 @@ impl<T, E> Result<T, E> {
494550 /////////////////////////////////////////////////////////////////////////
495551
496552 /// Returns an iterator over the possibly contained value.
553+ ///
554+ /// # Example
555+ ///
556+ /// ```
557+ /// let x: Result<uint, &str> = Ok(7);
558+ /// assert_eq!(x.iter().next(), Some(&7));
559+ ///
560+ /// let x: Result<uint, &str> = Err("nothing!");
561+ /// assert_eq!(x.iter().next(), None);
562+ /// ```
497563 #[ inline]
498564 #[ unstable = "waiting for iterator conventions" ]
499565 pub fn iter < ' r > ( & ' r self ) -> Item < & ' r T > {
@@ -507,6 +573,20 @@ impl<T, E> Result<T, E> {
507573 }
508574
509575 /// Returns a mutable iterator over the possibly contained value.
576+ ///
577+ /// # Example
578+ ///
579+ /// ```
580+ /// let mut x: Result<uint, &str> = Ok(7);
581+ /// match x.iter_mut().next() {
582+ /// Some(&ref mut x) => *x = 40,
583+ /// None => {},
584+ /// }
585+ /// assert_eq!(x, Ok(40));
586+ ///
587+ /// let mut x: Result<uint, &str> = Err("nothing!");
588+ /// assert_eq!(x.iter_mut().next(), None);
589+ /// ```
510590 #[ inline]
511591 #[ unstable = "waiting for iterator conventions" ]
512592 pub fn iter_mut < ' r > ( & ' r mut self ) -> Item < & ' r mut T > {
@@ -520,6 +600,18 @@ impl<T, E> Result<T, E> {
520600 }
521601
522602 /// Returns a consuming iterator over the possibly contained value.
603+ ///
604+ /// # Example
605+ ///
606+ /// ```
607+ /// let x: Result<uint, &str> = Ok(5);
608+ /// let v: Vec<uint> = x.into_iter().collect();
609+ /// assert_eq!(v, vec![5u]);
610+ ///
611+ /// let x: Result<uint, &str> = Err("nothing!");
612+ /// let v: Vec<uint> = x.into_iter().collect();
613+ /// assert_eq!(v, vec![]);
614+ /// ```
523615 #[ inline]
524616 #[ unstable = "waiting for iterator conventions" ]
525617 pub fn into_iter ( self ) -> Item < T > {
@@ -531,6 +623,26 @@ impl<T, E> Result<T, E> {
531623 /////////////////////////////////////////////////////////////////////////
532624
533625 /// Returns `res` if the result is `Ok`, otherwise returns the `Err` value of `self`.
626+ ///
627+ /// # Example
628+ ///
629+ /// ```
630+ /// let x: Result<uint, &str> = Ok(2);
631+ /// let y: Result<&str, &str> = Err("late error");
632+ /// assert_eq!(x.and(y), Err("late error"));
633+ ///
634+ /// let x: Result<uint, &str> = Err("early error");
635+ /// let y: Result<&str, &str> = Ok("foo");
636+ /// assert_eq!(x.and(y), Err("early error"));
637+ ///
638+ /// let x: Result<uint, &str> = Err("not a 2");
639+ /// let y: Result<&str, &str> = Err("late error");
640+ /// assert_eq!(x.and(y), Err("not a 2"));
641+ ///
642+ /// let x: Result<uint, &str> = Ok(2);
643+ /// let y: Result<&str, &str> = Ok("different result type");
644+ /// assert_eq!(x.and(y), Ok("different result type"));
645+ /// ```
534646 #[ inline]
535647 #[ stable]
536648 pub fn and < U > ( self , res : Result < U , E > ) -> Result < U , E > {
@@ -542,7 +654,19 @@ impl<T, E> Result<T, E> {
542654
543655 /// Calls `op` if the result is `Ok`, otherwise returns the `Err` value of `self`.
544656 ///
545- /// This function can be used for control flow based on result values
657+ /// This function can be used for control flow based on result values.
658+ ///
659+ /// # Example
660+ ///
661+ /// ```
662+ /// fn sq(x: uint) -> Result<uint, uint> { Ok(x * x) }
663+ /// fn err(x: uint) -> Result<uint, uint> { Err(x) }
664+ ///
665+ /// assert_eq!(Ok(2).and_then(sq).and_then(sq), Ok(16));
666+ /// assert_eq!(Ok(2).and_then(sq).and_then(err), Err(4));
667+ /// assert_eq!(Ok(2).and_then(err).and_then(sq), Err(2));
668+ /// assert_eq!(Err(3).and_then(sq).and_then(sq), Err(3));
669+ /// ```
546670 #[ inline]
547671 #[ unstable = "waiting for unboxed closures" ]
548672 pub fn and_then < U > ( self , op: |T | -> Result < U , E > ) -> Result < U , E > {
@@ -553,6 +677,26 @@ impl<T, E> Result<T, E> {
553677 }
554678
555679 /// Returns `res` if the result is `Err`, otherwise returns the `Ok` value of `self`.
680+ ///
681+ /// # Example
682+ ///
683+ /// ```
684+ /// let x: Result<uint, &str> = Ok(2);
685+ /// let y: Result<uint, &str> = Err("late error");
686+ /// assert_eq!(x.or(y), Ok(2));
687+ ///
688+ /// let x: Result<uint, &str> = Err("early error");
689+ /// let y: Result<uint, &str> = Ok(2);
690+ /// assert_eq!(x.or(y), Ok(2));
691+ ///
692+ /// let x: Result<uint, &str> = Err("not a 2");
693+ /// let y: Result<uint, &str> = Err("late error");
694+ /// assert_eq!(x.or(y), Err("late error"));
695+ ///
696+ /// let x: Result<uint, &str> = Ok(2);
697+ /// let y: Result<uint, &str> = Ok(100);
698+ /// assert_eq!(x.or(y), Ok(2));
699+ /// ```
556700 #[ inline]
557701 #[ stable]
558702 pub fn or( self , res : Result < T , E > ) -> Result < T , E > {
@@ -564,7 +708,19 @@ impl<T, E> Result<T, E> {
564708
565709 /// Calls `op` if the result is `Err`, otherwise returns the `Ok` value of `self`.
566710 ///
567- /// This function can be used for control flow based on result values
711+ /// This function can be used for control flow based on result values.
712+ ///
713+ /// # Example
714+ ///
715+ /// ```
716+ /// fn sq(x: uint) -> Result<uint, uint> { Ok(x * x) }
717+ /// fn err(x: uint) -> Result<uint, uint> { Err(x) }
718+ ///
719+ /// assert_eq!(Ok(2).or_else(sq).or_else(sq), Ok(2));
720+ /// assert_eq!(Ok(2).or_else(err).or_else(sq), Ok(2));
721+ /// assert_eq!(Err(3).or_else(sq).or_else(err), Ok(9));
722+ /// assert_eq!(Err(3).or_else(err).or_else(err), Err(3));
723+ /// ```
568724 #[ inline]
569725 #[ unstable = "waiting for unboxed closures" ]
570726 pub fn or_else < F > ( self , op: |E | -> Result < T , F > ) -> Result < T , F > {
@@ -576,6 +732,17 @@ impl<T, E> Result<T, E> {
576732
577733 /// Unwraps a result, yielding the content of an `Ok`.
578734 /// Else it returns `optb`.
735+ ///
736+ /// # Example
737+ ///
738+ /// ```
739+ /// let optb = 2u;
740+ /// let x: Result<uint, &str> = Ok(9u);
741+ /// assert_eq!(x.unwrap_or(optb), 9u);
742+ ///
743+ /// let x: Result<uint, &str> = Err("error");
744+ /// assert_eq!(x.unwrap_or(optb), optb);
745+ /// ```
579746 #[ inline]
580747 #[ unstable = "waiting for conventions" ]
581748 pub fn unwrap_or ( self , optb : T ) -> T {
@@ -587,6 +754,15 @@ impl<T, E> Result<T, E> {
587754
588755 /// Unwraps a result, yielding the content of an `Ok`.
589756 /// If the value is an `Err` then it calls `op` with its value.
757+ ///
758+ /// # Example
759+ ///
760+ /// ```
761+ /// fn count(x: &str) -> uint { x.len() }
762+ ///
763+ /// assert_eq!(Ok(2u).unwrap_or_else(count), 2u);
764+ /// assert_eq!(Err("foo").unwrap_or_else(count), 3u);
765+ /// ```
590766 #[ inline]
591767 #[ unstable = "waiting for conventions" ]
592768 pub fn unwrap_or_else ( self , op: |E | -> T ) -> T {
@@ -611,6 +787,18 @@ impl<T, E: Show> Result<T, E> {
611787 ///
612788 /// Fails if the value is an `Err`, with a custom failure message provided
613789 /// by the `Err`'s value.
790+ ///
791+ /// # Example
792+ ///
793+ /// ```
794+ /// let x: Result<uint, &str> = Ok(2u);
795+ /// assert_eq!(x.unwrap(), 2u);
796+ /// ```
797+ ///
798+ /// ```{.should_fail}
799+ /// let x: Result<uint, &str> = Err("emergency failure");
800+ /// x.unwrap(); // fails with `emergency failure`
801+ /// ```
614802 #[ inline]
615803 #[ unstable = "waiting for conventions" ]
616804 pub fn unwrap ( self ) -> T {
@@ -629,6 +817,18 @@ impl<T: Show, E> Result<T, E> {
629817 ///
630818 /// Fails if the value is an `Ok`, with a custom failure message provided
631819 /// by the `Ok`'s value.
820+ ///
821+ /// # Example
822+ ///
823+ /// ```{.should_fail}
824+ /// let x: Result<uint, &str> = Ok(2u);
825+ /// x.unwrap_err(); // fails with `2`
826+ /// ```
827+ ///
828+ /// ```
829+ /// let x: Result<uint, &str> = Err("emergency failure");
830+ /// assert_eq!(x.unwrap_err(), "emergency failure");
831+ /// ```
632832 #[ inline]
633833 #[ unstable = "waiting for conventions" ]
634834 pub fn unwrap_err ( self ) -> E {
@@ -666,7 +866,7 @@ impl<T, E> Slice<T> for Result<T, E> {
666866
667867/// A `Result` iterator that yields either one or zero elements
668868///
669- /// The `Item` iterator is returned by the `iter`, `mut_iter ` and `move_iter `
869+ /// The `Item` iterator is returned by the `iter`, `iter_mut ` and `into_iter `
670870/// methods on `Result`.
671871#[ deriving( Clone ) ]
672872#[ unstable = "waiting for iterator conventions" ]
0 commit comments