@@ -63,7 +63,7 @@ pub mod rt {
63
63
///
64
64
/// let pythagorean_triple = Triangle { a: 3.0, b: 4.0, c: 5.0 };
65
65
///
66
- /// println!( "{}", pythagorean_triple);
66
+ /// assert_eq!(format!( "{}", pythagorean_triple), "(3, 4, 5)" );
67
67
/// ```
68
68
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
69
69
pub type Result = result:: Result < ( ) , Error > ;
@@ -440,7 +440,7 @@ impl Display for Arguments<'_> {
440
440
///
441
441
/// let origin = Point { x: 0, y: 0 };
442
442
///
443
- /// println!( "The origin is: {:?}", origin);
443
+ /// assert_eq!(format!( "The origin is: {:?}", origin), "The origin is: Point { x: 0, y: 0 }" );
444
444
/// ```
445
445
///
446
446
/// Manually implementing:
@@ -464,22 +464,16 @@ impl Display for Arguments<'_> {
464
464
///
465
465
/// let origin = Point { x: 0, y: 0 };
466
466
///
467
- /// println!( "The origin is: {:?}", origin);
467
+ /// assert_eq!(format!( "The origin is: {:?}", origin), "The origin is: Point { x: 0, y: 0 }" );
468
468
/// ```
469
469
///
470
- /// This outputs:
471
- ///
472
- /// ```text
473
- /// The origin is: Point { x: 0, y: 0 }
474
- /// ```
475
- ///
476
- /// There are a number of `debug_*` methods on [`Formatter`] to help you with manual
477
- /// implementations, such as [`debug_struct`][debug_struct].
470
+ /// There are a number of helper methods on the [`Formatter`] struct to help you with manual
471
+ /// implementations, such as [`debug_struct`].
478
472
///
479
473
/// `Debug` implementations using either `derive` or the debug builder API
480
474
/// on [`Formatter`] support pretty-printing using the alternate flag: `{:#?}`.
481
475
///
482
- /// [debug_struct]: ../../std/fmt/struct.Formatter.html#method.debug_struct
476
+ /// [` debug_struct` ]: ../../std/fmt/struct.Formatter.html#method.debug_struct
483
477
/// [`Formatter`]: ../../std/fmt/struct.Formatter.html
484
478
///
485
479
/// Pretty-printing with `#?`:
@@ -493,17 +487,13 @@ impl Display for Arguments<'_> {
493
487
///
494
488
/// let origin = Point { x: 0, y: 0 };
495
489
///
496
- /// println!("The origin is: {:#?}", origin);
497
- /// ```
498
- ///
499
- /// This outputs:
500
- ///
501
- /// ```text
502
- /// The origin is: Point {
490
+ /// assert_eq!(format!("The origin is: {:#?}", origin),
491
+ /// "The origin is: Point {
503
492
/// x: 0,
504
- /// y: 0
505
- /// }
493
+ /// y: 0,
494
+ /// }");
506
495
/// ```
496
+
507
497
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
508
498
#[ rustc_on_unimplemented(
509
499
on(
@@ -538,8 +528,13 @@ pub trait Debug {
538
528
/// }
539
529
/// }
540
530
///
541
- /// assert_eq!("(1.987, 2.983)".to_owned(),
542
- /// format!("{:?}", Position { longitude: 1.987, latitude: 2.983, }));
531
+ /// let position = Position { longitude: 1.987, latitude: 2.983 };
532
+ /// assert_eq!(format!("{:?}", position), "(1.987, 2.983)");
533
+ ///
534
+ /// assert_eq!(format!("{:#?}", position), "(
535
+ /// 1.987,
536
+ /// 2.983,
537
+ /// )");
543
538
/// ```
544
539
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
545
540
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result ;
@@ -590,7 +585,7 @@ pub use macros::Debug;
590
585
///
591
586
/// let origin = Point { x: 0, y: 0 };
592
587
///
593
- /// println!( "The origin is: {}", origin);
588
+ /// assert_eq!(format!( "The origin is: {}", origin), "The origin is: (0, 0)" );
594
589
/// ```
595
590
#[ rustc_on_unimplemented(
596
591
on(
@@ -624,7 +619,7 @@ pub trait Display {
624
619
/// }
625
620
/// }
626
621
///
627
- /// assert_eq!("(1.987, 2.983)".to_owned() ,
622
+ /// assert_eq!("(1.987, 2.983)",
628
623
/// format!("{}", Position { longitude: 1.987, latitude: 2.983, }));
629
624
/// ```
630
625
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -674,7 +669,9 @@ pub trait Display {
674
669
///
675
670
/// let l = Length(9);
676
671
///
677
- /// println!("l as octal is: {:o}", l);
672
+ /// assert_eq!(format!("l as octal is: {:o}", l), "l as octal is: 11");
673
+ ///
674
+ /// assert_eq!(format!("l as octal is: {:#06o}", l), "l as octal is: 0o0011");
678
675
/// ```
679
676
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
680
677
pub trait Octal {
@@ -724,7 +721,12 @@ pub trait Octal {
724
721
///
725
722
/// let l = Length(107);
726
723
///
727
- /// println!("l as binary is: {:b}", l);
724
+ /// assert_eq!(format!("l as binary is: {:b}", l), "l as binary is: 1101011");
725
+ ///
726
+ /// assert_eq!(
727
+ /// format!("l as binary is: {:#032b}", l),
728
+ /// "l as binary is: 0b000000000000000000000001101011"
729
+ /// );
728
730
/// ```
729
731
///
730
732
/// [module]: ../../std/fmt/index.html
@@ -783,7 +785,9 @@ pub trait Binary {
783
785
///
784
786
/// let l = Length(9);
785
787
///
786
- /// println!("l as hex is: {:x}", l);
788
+ /// assert_eq!(format!("l as hex is: {:x}", l), "l as hex is: 9");
789
+ ///
790
+ /// assert_eq!(format!("l as hex is: {:#010x}", l), "l as hex is: 0x00000009");
787
791
/// ```
788
792
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
789
793
pub trait LowerHex {
@@ -834,9 +838,11 @@ pub trait LowerHex {
834
838
/// }
835
839
/// }
836
840
///
837
- /// let l = Length(9);
841
+ /// let l = Length(i32::max_value());
842
+ ///
843
+ /// assert_eq!(format!("l as hex is: {:X}", l), "l as hex is: 7FFFFFFF");
838
844
///
839
- /// println!( "l as hex is: {:X }", l);
845
+ /// assert_eq!(format!( "l as hex is: {:#010X }", l), "l as hex is: 0x7FFFFFFF" );
840
846
/// ```
841
847
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
842
848
pub trait UpperHex {
@@ -883,6 +889,10 @@ pub trait UpperHex {
883
889
/// let l = Length(42);
884
890
///
885
891
/// println!("l is in memory here: {:p}", l);
892
+ ///
893
+ /// let l_ptr = format!("{:018p}", l);
894
+ /// assert_eq!(l_ptr.len(), 18);
895
+ /// assert_eq!(&l_ptr[..2], "0x");
886
896
/// ```
887
897
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
888
898
pub trait Pointer {
@@ -925,7 +935,15 @@ pub trait Pointer {
925
935
///
926
936
/// let l = Length(100);
927
937
///
928
- /// println!("l in scientific notation is: {:e}", l);
938
+ /// assert_eq!(
939
+ /// format!("l in scientific notation is: {:e}", l),
940
+ /// "l in scientific notation is: 1e2"
941
+ /// );
942
+ ///
943
+ /// assert_eq!(
944
+ /// format!("l in scientific notation is: {:05e}", l),
945
+ /// "l in scientific notation is: 001e2"
946
+ /// );
929
947
/// ```
930
948
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
931
949
pub trait LowerExp {
@@ -968,7 +986,15 @@ pub trait LowerExp {
968
986
///
969
987
/// let l = Length(100);
970
988
///
971
- /// println!("l in scientific notation is: {:E}", l);
989
+ /// assert_eq!(
990
+ /// format!("l in scientific notation is: {:E}", l),
991
+ /// "l in scientific notation is: 1E2"
992
+ /// );
993
+ ///
994
+ /// assert_eq!(
995
+ /// format!("l in scientific notation is: {:05E}", l),
996
+ /// "l in scientific notation is: 001E2"
997
+ /// );
972
998
/// ```
973
999
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
974
1000
pub trait UpperExp {
@@ -1813,8 +1839,7 @@ impl<'a> Formatter<'a> {
1813
1839
/// }
1814
1840
/// }
1815
1841
///
1816
- /// // prints "[10, 11]"
1817
- /// println!("{:?}", Foo(vec![10, 11]));
1842
+ /// assert_eq!(format!("{:?}", Foo(vec![10, 11])), "[10, 11]");
1818
1843
/// ```
1819
1844
#[ stable( feature = "debug_builders" , since = "1.2.0" ) ]
1820
1845
pub fn debug_list < ' b > ( & ' b mut self ) -> DebugList < ' b , ' a > {
@@ -1837,8 +1862,7 @@ impl<'a> Formatter<'a> {
1837
1862
/// }
1838
1863
/// }
1839
1864
///
1840
- /// // prints "{10, 11}"
1841
- /// println!("{:?}", Foo(vec![10, 11]));
1865
+ /// assert_eq!(format!("{:?}", Foo(vec![10, 11])), "{10, 11}");
1842
1866
/// ```
1843
1867
///
1844
1868
/// [`format_args!`]: ../../std/macro.format_args.html
@@ -1896,8 +1920,10 @@ impl<'a> Formatter<'a> {
1896
1920
/// }
1897
1921
/// }
1898
1922
///
1899
- /// // prints "{"A": 10, "B": 11}"
1900
- /// println!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)]));
1923
+ /// assert_eq!(
1924
+ /// format!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)])),
1925
+ /// r#"{"A": 10, "B": 11}"#
1926
+ /// );
1901
1927
/// ```
1902
1928
#[ stable( feature = "debug_builders" , since = "1.2.0" ) ]
1903
1929
pub fn debug_map < ' b > ( & ' b mut self ) -> DebugMap < ' b , ' a > {
0 commit comments