@@ -311,14 +311,13 @@ impl<T, E> Result<T, E> {
311
311
///
312
312
/// # Example
313
313
///
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
+ /// ```
322
321
#[ inline]
323
322
#[ stable]
324
323
pub fn is_ok ( & self ) -> bool {
@@ -332,14 +331,13 @@ impl<T, E> Result<T, E> {
332
331
///
333
332
/// # Example
334
333
///
335
- /// ~~~
336
- /// use std::io::{File, Open, Read};
334
+ /// ```
335
+ /// let x: Result<int, &str> = Ok(-3);
336
+ /// assert_eq!(x.is_err(), false);
337
337
///
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
+ /// ```
343
341
#[ inline]
344
342
#[ stable]
345
343
pub fn is_err ( & self ) -> bool {
@@ -356,18 +354,15 @@ impl<T, E> Result<T, E> {
356
354
/// Converts `self` into an `Option<T>`, consuming `self`,
357
355
/// and discarding the error, if any.
358
356
///
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
364
358
///
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));
367
362
///
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
+ /// ```
371
366
#[ inline]
372
367
#[ stable]
373
368
pub fn ok ( self ) -> Option < T > {
@@ -381,6 +376,16 @@ impl<T, E> Result<T, E> {
381
376
///
382
377
/// Converts `self` into an `Option<T>`, consuming `self`,
383
378
/// 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
+ /// ```
384
389
#[ inline]
385
390
#[ stable]
386
391
pub fn err ( self ) -> Option < E > {
@@ -398,6 +403,14 @@ impl<T, E> Result<T, E> {
398
403
///
399
404
/// Produces a new `Result`, containing a reference
400
405
/// 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
+ /// ```
401
414
#[ inline]
402
415
#[ stable]
403
416
pub fn as_ref < ' r > ( & ' r self ) -> Result < & ' r T , & ' r E > {
@@ -408,6 +421,23 @@ impl<T, E> Result<T, E> {
408
421
}
409
422
410
423
/// 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
+ /// ```
411
441
#[ inline]
412
442
#[ unstable = "waiting for mut conventions" ]
413
443
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> {
418
448
}
419
449
420
450
/// 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
+ /// ```
421
465
#[ inline]
422
466
#[ unstable = "waiting for mut conventions" ]
423
467
pub fn as_mut_slice < ' r > ( & ' r mut self ) -> & ' r mut [ T ] {
@@ -479,6 +523,18 @@ impl<T, E> Result<T, E> {
479
523
///
480
524
/// This function can be used to pass through a successful result while handling
481
525
/// 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
+ /// ```
482
538
#[ inline]
483
539
#[ unstable = "waiting for unboxed closures" ]
484
540
pub fn map_err < F > ( self , op: |E | -> F ) -> Result < T , F > {
@@ -494,6 +550,16 @@ impl<T, E> Result<T, E> {
494
550
/////////////////////////////////////////////////////////////////////////
495
551
496
552
/// 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
+ /// ```
497
563
#[ inline]
498
564
#[ unstable = "waiting for iterator conventions" ]
499
565
pub fn iter < ' r > ( & ' r self ) -> Item < & ' r T > {
@@ -507,6 +573,20 @@ impl<T, E> Result<T, E> {
507
573
}
508
574
509
575
/// 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
+ /// ```
510
590
#[ inline]
511
591
#[ unstable = "waiting for iterator conventions" ]
512
592
pub fn iter_mut < ' r > ( & ' r mut self ) -> Item < & ' r mut T > {
@@ -520,6 +600,18 @@ impl<T, E> Result<T, E> {
520
600
}
521
601
522
602
/// 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
+ /// ```
523
615
#[ inline]
524
616
#[ unstable = "waiting for iterator conventions" ]
525
617
pub fn into_iter ( self ) -> Item < T > {
@@ -531,6 +623,26 @@ impl<T, E> Result<T, E> {
531
623
/////////////////////////////////////////////////////////////////////////
532
624
533
625
/// 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
+ /// ```
534
646
#[ inline]
535
647
#[ stable]
536
648
pub fn and < U > ( self , res : Result < U , E > ) -> Result < U , E > {
@@ -542,7 +654,19 @@ impl<T, E> Result<T, E> {
542
654
543
655
/// Calls `op` if the result is `Ok`, otherwise returns the `Err` value of `self`.
544
656
///
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
+ /// ```
546
670
#[ inline]
547
671
#[ unstable = "waiting for unboxed closures" ]
548
672
pub fn and_then < U > ( self , op: |T | -> Result < U , E > ) -> Result < U , E > {
@@ -553,6 +677,26 @@ impl<T, E> Result<T, E> {
553
677
}
554
678
555
679
/// 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
+ /// ```
556
700
#[ inline]
557
701
#[ stable]
558
702
pub fn or( self , res : Result < T , E > ) -> Result < T , E > {
@@ -564,7 +708,19 @@ impl<T, E> Result<T, E> {
564
708
565
709
/// Calls `op` if the result is `Err`, otherwise returns the `Ok` value of `self`.
566
710
///
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
+ /// ```
568
724
#[ inline]
569
725
#[ unstable = "waiting for unboxed closures" ]
570
726
pub fn or_else < F > ( self , op: |E | -> Result < T , F > ) -> Result < T , F > {
@@ -576,6 +732,17 @@ impl<T, E> Result<T, E> {
576
732
577
733
/// Unwraps a result, yielding the content of an `Ok`.
578
734
/// 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
+ /// ```
579
746
#[ inline]
580
747
#[ unstable = "waiting for conventions" ]
581
748
pub fn unwrap_or ( self , optb : T ) -> T {
@@ -587,6 +754,15 @@ impl<T, E> Result<T, E> {
587
754
588
755
/// Unwraps a result, yielding the content of an `Ok`.
589
756
/// 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
+ /// ```
590
766
#[ inline]
591
767
#[ unstable = "waiting for conventions" ]
592
768
pub fn unwrap_or_else ( self , op: |E | -> T ) -> T {
@@ -611,6 +787,18 @@ impl<T, E: Show> Result<T, E> {
611
787
///
612
788
/// Fails if the value is an `Err`, with a custom failure message provided
613
789
/// 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
+ /// ```
614
802
#[ inline]
615
803
#[ unstable = "waiting for conventions" ]
616
804
pub fn unwrap ( self ) -> T {
@@ -629,6 +817,18 @@ impl<T: Show, E> Result<T, E> {
629
817
///
630
818
/// Fails if the value is an `Ok`, with a custom failure message provided
631
819
/// 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
+ /// ```
632
832
#[ inline]
633
833
#[ unstable = "waiting for conventions" ]
634
834
pub fn unwrap_err ( self ) -> E {
@@ -666,7 +866,7 @@ impl<T, E> Slice<T> for Result<T, E> {
666
866
667
867
/// A `Result` iterator that yields either one or zero elements
668
868
///
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 `
670
870
/// methods on `Result`.
671
871
#[ deriving( Clone ) ]
672
872
#[ unstable = "waiting for iterator conventions" ]
0 commit comments