@@ -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:
@@ -455,28 +455,25 @@ impl Display for Arguments<'_> {
455
455
///
456
456
/// impl fmt::Debug for Point {
457
457
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
458
- /// write!(f, "Point {{ x: {}, y: {} }}", self.x, self.y)
458
+ /// f.debug_struct("Point")
459
+ /// .field("x", &self.x)
460
+ /// .field("y", &self.y)
461
+ /// .finish()
459
462
/// }
460
463
/// }
461
464
///
462
465
/// let origin = Point { x: 0, y: 0 };
463
466
///
464
- /// println!( "The origin is: {:?}", origin);
467
+ /// assert_eq!(format!( "The origin is: {:?}", origin), "The origin is: Point { x: 0, y: 0 }" );
465
468
/// ```
466
469
///
467
- /// This outputs:
468
- ///
469
- /// ```text
470
- /// The origin is: Point { x: 0, y: 0 }
471
- /// ```
472
- ///
473
- /// There are a number of `debug_*` methods on [`Formatter`] to help you with manual
474
- /// 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`].
475
472
///
476
473
/// `Debug` implementations using either `derive` or the debug builder API
477
474
/// on [`Formatter`] support pretty-printing using the alternate flag: `{:#?}`.
478
475
///
479
- /// [debug_struct]: ../../std/fmt/struct.Formatter.html#method.debug_struct
476
+ /// [` debug_struct` ]: ../../std/fmt/struct.Formatter.html#method.debug_struct
480
477
/// [`Formatter`]: ../../std/fmt/struct.Formatter.html
481
478
///
482
479
/// Pretty-printing with `#?`:
@@ -490,17 +487,13 @@ impl Display for Arguments<'_> {
490
487
///
491
488
/// let origin = Point { x: 0, y: 0 };
492
489
///
493
- /// println!("The origin is: {:#?}", origin);
494
- /// ```
495
- ///
496
- /// This outputs:
497
- ///
498
- /// ```text
499
- /// The origin is: Point {
490
+ /// assert_eq!(format!("The origin is: {:#?}", origin),
491
+ /// "The origin is: Point {
500
492
/// x: 0,
501
- /// y: 0
502
- /// }
493
+ /// y: 0,
494
+ /// }");
503
495
/// ```
496
+
504
497
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
505
498
#[ rustc_on_unimplemented(
506
499
on(
@@ -528,12 +521,20 @@ pub trait Debug {
528
521
///
529
522
/// impl fmt::Debug for Position {
530
523
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
531
- /// write!(f, "({:?}, {:?})", self.longitude, self.latitude)
524
+ /// f.debug_tuple("")
525
+ /// .field(&self.longitude)
526
+ /// .field(&self.latitude)
527
+ /// .finish()
532
528
/// }
533
529
/// }
534
530
///
535
- /// assert_eq!("(1.987, 2.983)".to_owned(),
536
- /// 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
+ /// )");
537
538
/// ```
538
539
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
539
540
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result ;
@@ -584,7 +585,7 @@ pub use macros::Debug;
584
585
///
585
586
/// let origin = Point { x: 0, y: 0 };
586
587
///
587
- /// println!( "The origin is: {}", origin);
588
+ /// assert_eq!(format!( "The origin is: {}", origin), "The origin is: (0, 0)" );
588
589
/// ```
589
590
#[ rustc_on_unimplemented(
590
591
on(
@@ -618,7 +619,7 @@ pub trait Display {
618
619
/// }
619
620
/// }
620
621
///
621
- /// assert_eq!("(1.987, 2.983)".to_owned() ,
622
+ /// assert_eq!("(1.987, 2.983)",
622
623
/// format!("{}", Position { longitude: 1.987, latitude: 2.983, }));
623
624
/// ```
624
625
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -668,7 +669,9 @@ pub trait Display {
668
669
///
669
670
/// let l = Length(9);
670
671
///
671
- /// 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");
672
675
/// ```
673
676
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
674
677
pub trait Octal {
@@ -718,7 +721,12 @@ pub trait Octal {
718
721
///
719
722
/// let l = Length(107);
720
723
///
721
- /// 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
+ /// );
722
730
/// ```
723
731
///
724
732
/// [module]: ../../std/fmt/index.html
@@ -777,7 +785,9 @@ pub trait Binary {
777
785
///
778
786
/// let l = Length(9);
779
787
///
780
- /// 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");
781
791
/// ```
782
792
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
783
793
pub trait LowerHex {
@@ -828,9 +838,11 @@ pub trait LowerHex {
828
838
/// }
829
839
/// }
830
840
///
831
- /// let l = Length(9 );
841
+ /// let l = Length(i32::max_value() );
832
842
///
833
- /// println!("l as hex is: {:X}", l);
843
+ /// assert_eq!(format!("l as hex is: {:X}", l), "l as hex is: 7FFFFFFF");
844
+ ///
845
+ /// assert_eq!(format!("l as hex is: {:#010X}", l), "l as hex is: 0x7FFFFFFF");
834
846
/// ```
835
847
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
836
848
pub trait UpperHex {
@@ -877,6 +889,10 @@ pub trait UpperHex {
877
889
/// let l = Length(42);
878
890
///
879
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");
880
896
/// ```
881
897
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
882
898
pub trait Pointer {
@@ -912,14 +928,22 @@ pub trait Pointer {
912
928
///
913
929
/// impl fmt::LowerExp for Length {
914
930
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
915
- /// let val = self.0;
916
- /// write!(f, "{}e1", val / 10)
931
+ /// let val = f64::from( self.0) ;
932
+ /// fmt::LowerExp::fmt(&val, f) // delegate to f64's implementation
917
933
/// }
918
934
/// }
919
935
///
920
936
/// let l = Length(100);
921
937
///
922
- /// 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
+ /// );
923
947
/// ```
924
948
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
925
949
pub trait LowerExp {
@@ -955,14 +979,22 @@ pub trait LowerExp {
955
979
///
956
980
/// impl fmt::UpperExp for Length {
957
981
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
958
- /// let val = self.0;
959
- /// write!(f, "{}E1", val / 10)
982
+ /// let val = f64::from( self.0) ;
983
+ /// fmt::UpperExp::fmt(&val, f) // delegate to f64's implementation
960
984
/// }
961
985
/// }
962
986
///
963
987
/// let l = Length(100);
964
988
///
965
- /// 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
+ /// );
966
998
/// ```
967
999
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
968
1000
pub trait UpperExp {
@@ -1807,8 +1839,7 @@ impl<'a> Formatter<'a> {
1807
1839
/// }
1808
1840
/// }
1809
1841
///
1810
- /// // prints "[10, 11]"
1811
- /// println!("{:?}", Foo(vec![10, 11]));
1842
+ /// assert_eq!(format!("{:?}", Foo(vec![10, 11])), "[10, 11]");
1812
1843
/// ```
1813
1844
#[ stable( feature = "debug_builders" , since = "1.2.0" ) ]
1814
1845
pub fn debug_list < ' b > ( & ' b mut self ) -> DebugList < ' b , ' a > {
@@ -1831,8 +1862,7 @@ impl<'a> Formatter<'a> {
1831
1862
/// }
1832
1863
/// }
1833
1864
///
1834
- /// // prints "{10, 11}"
1835
- /// println!("{:?}", Foo(vec![10, 11]));
1865
+ /// assert_eq!(format!("{:?}", Foo(vec![10, 11])), "{10, 11}");
1836
1866
/// ```
1837
1867
///
1838
1868
/// [`format_args!`]: ../../std/macro.format_args.html
@@ -1890,8 +1920,10 @@ impl<'a> Formatter<'a> {
1890
1920
/// }
1891
1921
/// }
1892
1922
///
1893
- /// // prints "{"A": 10, "B": 11}"
1894
- /// 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
+ /// );
1895
1927
/// ```
1896
1928
#[ stable( feature = "debug_builders" , since = "1.2.0" ) ]
1897
1929
pub fn debug_map < ' b > ( & ' b mut self ) -> DebugMap < ' b , ' a > {
0 commit comments