@@ -63,7 +63,7 @@ pub mod rt {
6363///
6464/// let pythagorean_triple = Triangle { a: 3.0, b: 4.0, c: 5.0 };
6565///
66- /// println!( "{}", pythagorean_triple);
66+ /// assert_eq!(format!( "{}", pythagorean_triple), "(3, 4, 5)" );
6767/// ```
6868#[ stable( feature = "rust1" , since = "1.0.0" ) ]
6969pub type Result = result:: Result < ( ) , Error > ;
@@ -440,7 +440,7 @@ impl Display for Arguments<'_> {
440440///
441441/// let origin = Point { x: 0, y: 0 };
442442///
443- /// println!( "The origin is: {:?}", origin);
443+ /// assert_eq!(format!( "The origin is: {:?}", origin), "The origin is: Point { x: 0, y: 0 }" );
444444/// ```
445445///
446446/// Manually implementing:
@@ -455,28 +455,25 @@ impl Display for Arguments<'_> {
455455///
456456/// impl fmt::Debug for Point {
457457/// 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()
459462/// }
460463/// }
461464///
462465/// let origin = Point { x: 0, y: 0 };
463466///
464- /// println!( "The origin is: {:?}", origin);
467+ /// assert_eq!(format!( "The origin is: {:?}", origin), "The origin is: Point { x: 0, y: 0 }" );
465468/// ```
466469///
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`].
475472///
476473/// `Debug` implementations using either `derive` or the debug builder API
477474/// on [`Formatter`] support pretty-printing using the alternate flag: `{:#?}`.
478475///
479- /// [debug_struct]: ../../std/fmt/struct.Formatter.html#method.debug_struct
476+ /// [` debug_struct` ]: ../../std/fmt/struct.Formatter.html#method.debug_struct
480477/// [`Formatter`]: ../../std/fmt/struct.Formatter.html
481478///
482479/// Pretty-printing with `#?`:
@@ -490,17 +487,13 @@ impl Display for Arguments<'_> {
490487///
491488/// let origin = Point { x: 0, y: 0 };
492489///
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 {
500492/// x: 0,
501- /// y: 0
502- /// }
493+ /// y: 0,
494+ /// }");
503495/// ```
496+
504497#[ stable( feature = "rust1" , since = "1.0.0" ) ]
505498#[ rustc_on_unimplemented(
506499 on(
@@ -528,12 +521,20 @@ pub trait Debug {
528521 ///
529522 /// impl fmt::Debug for Position {
530523 /// 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()
532528 /// }
533529 /// }
534530 ///
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+ /// )");
537538 /// ```
538539 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
539540 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result ;
@@ -584,7 +585,7 @@ pub use macros::Debug;
584585///
585586/// let origin = Point { x: 0, y: 0 };
586587///
587- /// println!( "The origin is: {}", origin);
588+ /// assert_eq!(format!( "The origin is: {}", origin), "The origin is: (0, 0)" );
588589/// ```
589590#[ rustc_on_unimplemented(
590591 on(
@@ -618,7 +619,7 @@ pub trait Display {
618619 /// }
619620 /// }
620621 ///
621- /// assert_eq!("(1.987, 2.983)".to_owned() ,
622+ /// assert_eq!("(1.987, 2.983)",
622623 /// format!("{}", Position { longitude: 1.987, latitude: 2.983, }));
623624 /// ```
624625 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -668,7 +669,9 @@ pub trait Display {
668669///
669670/// let l = Length(9);
670671///
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");
672675/// ```
673676#[ stable( feature = "rust1" , since = "1.0.0" ) ]
674677pub trait Octal {
@@ -718,7 +721,12 @@ pub trait Octal {
718721///
719722/// let l = Length(107);
720723///
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+ /// );
722730/// ```
723731///
724732/// [module]: ../../std/fmt/index.html
@@ -777,7 +785,9 @@ pub trait Binary {
777785///
778786/// let l = Length(9);
779787///
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");
781791/// ```
782792#[ stable( feature = "rust1" , since = "1.0.0" ) ]
783793pub trait LowerHex {
@@ -828,9 +838,11 @@ pub trait LowerHex {
828838/// }
829839/// }
830840///
831- /// let l = Length(9 );
841+ /// let l = Length(i32::max_value() );
832842///
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");
834846/// ```
835847#[ stable( feature = "rust1" , since = "1.0.0" ) ]
836848pub trait UpperHex {
@@ -877,6 +889,10 @@ pub trait UpperHex {
877889/// let l = Length(42);
878890///
879891/// 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");
880896/// ```
881897#[ stable( feature = "rust1" , since = "1.0.0" ) ]
882898pub trait Pointer {
@@ -912,14 +928,22 @@ pub trait Pointer {
912928///
913929/// impl fmt::LowerExp for Length {
914930/// 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
917933/// }
918934/// }
919935///
920936/// let l = Length(100);
921937///
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+ /// );
923947/// ```
924948#[ stable( feature = "rust1" , since = "1.0.0" ) ]
925949pub trait LowerExp {
@@ -955,14 +979,22 @@ pub trait LowerExp {
955979///
956980/// impl fmt::UpperExp for Length {
957981/// 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
960984/// }
961985/// }
962986///
963987/// let l = Length(100);
964988///
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+ /// );
966998/// ```
967999#[ stable( feature = "rust1" , since = "1.0.0" ) ]
9681000pub trait UpperExp {
@@ -1807,8 +1839,7 @@ impl<'a> Formatter<'a> {
18071839 /// }
18081840 /// }
18091841 ///
1810- /// // prints "[10, 11]"
1811- /// println!("{:?}", Foo(vec![10, 11]));
1842+ /// assert_eq!(format!("{:?}", Foo(vec![10, 11])), "[10, 11]");
18121843 /// ```
18131844 #[ stable( feature = "debug_builders" , since = "1.2.0" ) ]
18141845 pub fn debug_list < ' b > ( & ' b mut self ) -> DebugList < ' b , ' a > {
@@ -1831,8 +1862,7 @@ impl<'a> Formatter<'a> {
18311862 /// }
18321863 /// }
18331864 ///
1834- /// // prints "{10, 11}"
1835- /// println!("{:?}", Foo(vec![10, 11]));
1865+ /// assert_eq!(format!("{:?}", Foo(vec![10, 11])), "{10, 11}");
18361866 /// ```
18371867 ///
18381868 /// [`format_args!`]: ../../std/macro.format_args.html
@@ -1890,8 +1920,10 @@ impl<'a> Formatter<'a> {
18901920 /// }
18911921 /// }
18921922 ///
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+ /// );
18951927 /// ```
18961928 #[ stable( feature = "debug_builders" , since = "1.2.0" ) ]
18971929 pub fn debug_map < ' b > ( & ' b mut self ) -> DebugMap < ' b , ' a > {
0 commit comments