Skip to content

Commit a9d6889

Browse files
committed
Replace prints in fmt docs with asserts
1 parent 0cc8fe5 commit a9d6889

File tree

1 file changed

+64
-38
lines changed

1 file changed

+64
-38
lines changed

src/libcore/fmt/mod.rs

+64-38
Original file line numberDiff line numberDiff line change
@@ -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")]
6969
pub 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:
@@ -464,22 +464,16 @@ impl Display for Arguments<'_> {
464464
///
465465
/// let origin = Point { x: 0, y: 0 };
466466
///
467-
/// println!("The origin is: {:?}", origin);
467+
/// assert_eq!(format!("The origin is: {:?}", origin), "The origin is: Point { x: 0, y: 0 }");
468468
/// ```
469469
///
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`].
478472
///
479473
/// `Debug` implementations using either `derive` or the debug builder API
480474
/// on [`Formatter`] support pretty-printing using the alternate flag: `{:#?}`.
481475
///
482-
/// [debug_struct]: ../../std/fmt/struct.Formatter.html#method.debug_struct
476+
/// [`debug_struct`]: ../../std/fmt/struct.Formatter.html#method.debug_struct
483477
/// [`Formatter`]: ../../std/fmt/struct.Formatter.html
484478
///
485479
/// Pretty-printing with `#?`:
@@ -493,17 +487,13 @@ impl Display for Arguments<'_> {
493487
///
494488
/// let origin = Point { x: 0, y: 0 };
495489
///
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 {
503492
/// x: 0,
504-
/// y: 0
505-
/// }
493+
/// y: 0,
494+
/// }");
506495
/// ```
496+
507497
#[stable(feature = "rust1", since = "1.0.0")]
508498
#[rustc_on_unimplemented(
509499
on(
@@ -538,8 +528,13 @@ pub trait Debug {
538528
/// }
539529
/// }
540530
///
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+
/// )");
543538
/// ```
544539
#[stable(feature = "rust1", since = "1.0.0")]
545540
fn fmt(&self, f: &mut Formatter<'_>) -> Result;
@@ -590,7 +585,7 @@ pub use macros::Debug;
590585
///
591586
/// let origin = Point { x: 0, y: 0 };
592587
///
593-
/// println!("The origin is: {}", origin);
588+
/// assert_eq!(format!("The origin is: {}", origin), "The origin is: (0, 0)");
594589
/// ```
595590
#[rustc_on_unimplemented(
596591
on(
@@ -624,7 +619,7 @@ pub trait Display {
624619
/// }
625620
/// }
626621
///
627-
/// assert_eq!("(1.987, 2.983)".to_owned(),
622+
/// assert_eq!("(1.987, 2.983)",
628623
/// format!("{}", Position { longitude: 1.987, latitude: 2.983, }));
629624
/// ```
630625
#[stable(feature = "rust1", since = "1.0.0")]
@@ -674,7 +669,9 @@ pub trait Display {
674669
///
675670
/// let l = Length(9);
676671
///
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");
678675
/// ```
679676
#[stable(feature = "rust1", since = "1.0.0")]
680677
pub trait Octal {
@@ -724,7 +721,12 @@ pub trait Octal {
724721
///
725722
/// let l = Length(107);
726723
///
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+
/// );
728730
/// ```
729731
///
730732
/// [module]: ../../std/fmt/index.html
@@ -783,7 +785,9 @@ pub trait Binary {
783785
///
784786
/// let l = Length(9);
785787
///
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");
787791
/// ```
788792
#[stable(feature = "rust1", since = "1.0.0")]
789793
pub trait LowerHex {
@@ -834,9 +838,11 @@ pub trait LowerHex {
834838
/// }
835839
/// }
836840
///
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");
838844
///
839-
/// println!("l as hex is: {:X}", l);
845+
/// assert_eq!(format!("l as hex is: {:#010X}", l), "l as hex is: 0x7FFFFFFF");
840846
/// ```
841847
#[stable(feature = "rust1", since = "1.0.0")]
842848
pub trait UpperHex {
@@ -883,6 +889,10 @@ pub trait UpperHex {
883889
/// let l = Length(42);
884890
///
885891
/// 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");
886896
/// ```
887897
#[stable(feature = "rust1", since = "1.0.0")]
888898
pub trait Pointer {
@@ -925,7 +935,15 @@ pub trait Pointer {
925935
///
926936
/// let l = Length(100);
927937
///
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+
/// );
929947
/// ```
930948
#[stable(feature = "rust1", since = "1.0.0")]
931949
pub trait LowerExp {
@@ -968,7 +986,15 @@ pub trait LowerExp {
968986
///
969987
/// let l = Length(100);
970988
///
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+
/// );
972998
/// ```
973999
#[stable(feature = "rust1", since = "1.0.0")]
9741000
pub trait UpperExp {
@@ -1813,8 +1839,7 @@ impl<'a> Formatter<'a> {
18131839
/// }
18141840
/// }
18151841
///
1816-
/// // prints "[10, 11]"
1817-
/// println!("{:?}", Foo(vec![10, 11]));
1842+
/// assert_eq!(format!("{:?}", Foo(vec![10, 11])), "[10, 11]");
18181843
/// ```
18191844
#[stable(feature = "debug_builders", since = "1.2.0")]
18201845
pub fn debug_list<'b>(&'b mut self) -> DebugList<'b, 'a> {
@@ -1837,8 +1862,7 @@ impl<'a> Formatter<'a> {
18371862
/// }
18381863
/// }
18391864
///
1840-
/// // prints "{10, 11}"
1841-
/// println!("{:?}", Foo(vec![10, 11]));
1865+
/// assert_eq!(format!("{:?}", Foo(vec![10, 11])), "{10, 11}");
18421866
/// ```
18431867
///
18441868
/// [`format_args!`]: ../../std/macro.format_args.html
@@ -1896,8 +1920,10 @@ impl<'a> Formatter<'a> {
18961920
/// }
18971921
/// }
18981922
///
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+
/// );
19011927
/// ```
19021928
#[stable(feature = "debug_builders", since = "1.2.0")]
19031929
pub fn debug_map<'b>(&'b mut self) -> DebugMap<'b, 'a> {

0 commit comments

Comments
 (0)