Skip to content

Commit

Permalink
Replace prints in fmt docs with asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
elichai committed Dec 15, 2019
1 parent 0cc8fe5 commit a9d6889
Showing 1 changed file with 64 additions and 38 deletions.
102 changes: 64 additions & 38 deletions src/libcore/fmt/mod.rs
Expand Up @@ -63,7 +63,7 @@ pub mod rt {
///
/// let pythagorean_triple = Triangle { a: 3.0, b: 4.0, c: 5.0 };
///
/// println!("{}", pythagorean_triple);
/// assert_eq!(format!("{}", pythagorean_triple), "(3, 4, 5)");
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub type Result = result::Result<(), Error>;
Expand Down Expand Up @@ -440,7 +440,7 @@ impl Display for Arguments<'_> {
///
/// let origin = Point { x: 0, y: 0 };
///
/// println!("The origin is: {:?}", origin);
/// assert_eq!(format!("The origin is: {:?}", origin), "The origin is: Point { x: 0, y: 0 }");
/// ```
///
/// Manually implementing:
Expand All @@ -464,22 +464,16 @@ impl Display for Arguments<'_> {
///
/// let origin = Point { x: 0, y: 0 };
///
/// println!("The origin is: {:?}", origin);
/// assert_eq!(format!("The origin is: {:?}", origin), "The origin is: Point { x: 0, y: 0 }");
/// ```
///
/// This outputs:
///
/// ```text
/// The origin is: Point { x: 0, y: 0 }
/// ```
///
/// There are a number of `debug_*` methods on [`Formatter`] to help you with manual
/// implementations, such as [`debug_struct`][debug_struct].
/// There are a number of helper methods on the [`Formatter`] struct to help you with manual
/// implementations, such as [`debug_struct`].
///
/// `Debug` implementations using either `derive` or the debug builder API
/// on [`Formatter`] support pretty-printing using the alternate flag: `{:#?}`.
///
/// [debug_struct]: ../../std/fmt/struct.Formatter.html#method.debug_struct
/// [`debug_struct`]: ../../std/fmt/struct.Formatter.html#method.debug_struct
/// [`Formatter`]: ../../std/fmt/struct.Formatter.html
///
/// Pretty-printing with `#?`:
Expand All @@ -493,17 +487,13 @@ impl Display for Arguments<'_> {
///
/// let origin = Point { x: 0, y: 0 };
///
/// println!("The origin is: {:#?}", origin);
/// ```
///
/// This outputs:
///
/// ```text
/// The origin is: Point {
/// assert_eq!(format!("The origin is: {:#?}", origin),
/// "The origin is: Point {
/// x: 0,
/// y: 0
/// }
/// y: 0,
/// }");
/// ```

#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_on_unimplemented(
on(
Expand Down Expand Up @@ -538,8 +528,13 @@ pub trait Debug {
/// }
/// }
///
/// assert_eq!("(1.987, 2.983)".to_owned(),
/// format!("{:?}", Position { longitude: 1.987, latitude: 2.983, }));
/// let position = Position { longitude: 1.987, latitude: 2.983 };
/// assert_eq!(format!("{:?}", position), "(1.987, 2.983)");
///
/// assert_eq!(format!("{:#?}", position), "(
/// 1.987,
/// 2.983,
/// )");
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn fmt(&self, f: &mut Formatter<'_>) -> Result;
Expand Down Expand Up @@ -590,7 +585,7 @@ pub use macros::Debug;
///
/// let origin = Point { x: 0, y: 0 };
///
/// println!("The origin is: {}", origin);
/// assert_eq!(format!("The origin is: {}", origin), "The origin is: (0, 0)");
/// ```
#[rustc_on_unimplemented(
on(
Expand Down Expand Up @@ -624,7 +619,7 @@ pub trait Display {
/// }
/// }
///
/// assert_eq!("(1.987, 2.983)".to_owned(),
/// assert_eq!("(1.987, 2.983)",
/// format!("{}", Position { longitude: 1.987, latitude: 2.983, }));
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -674,7 +669,9 @@ pub trait Display {
///
/// let l = Length(9);
///
/// println!("l as octal is: {:o}", l);
/// assert_eq!(format!("l as octal is: {:o}", l), "l as octal is: 11");
///
/// assert_eq!(format!("l as octal is: {:#06o}", l), "l as octal is: 0o0011");
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub trait Octal {
Expand Down Expand Up @@ -724,7 +721,12 @@ pub trait Octal {
///
/// let l = Length(107);
///
/// println!("l as binary is: {:b}", l);
/// assert_eq!(format!("l as binary is: {:b}", l), "l as binary is: 1101011");
///
/// assert_eq!(
/// format!("l as binary is: {:#032b}", l),
/// "l as binary is: 0b000000000000000000000001101011"
/// );
/// ```
///
/// [module]: ../../std/fmt/index.html
Expand Down Expand Up @@ -783,7 +785,9 @@ pub trait Binary {
///
/// let l = Length(9);
///
/// println!("l as hex is: {:x}", l);
/// assert_eq!(format!("l as hex is: {:x}", l), "l as hex is: 9");
///
/// assert_eq!(format!("l as hex is: {:#010x}", l), "l as hex is: 0x00000009");
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub trait LowerHex {
Expand Down Expand Up @@ -834,9 +838,11 @@ pub trait LowerHex {
/// }
/// }
///
/// let l = Length(9);
/// let l = Length(i32::max_value());
///
/// assert_eq!(format!("l as hex is: {:X}", l), "l as hex is: 7FFFFFFF");
///
/// println!("l as hex is: {:X}", l);
/// assert_eq!(format!("l as hex is: {:#010X}", l), "l as hex is: 0x7FFFFFFF");
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub trait UpperHex {
Expand Down Expand Up @@ -883,6 +889,10 @@ pub trait UpperHex {
/// let l = Length(42);
///
/// println!("l is in memory here: {:p}", l);
///
/// let l_ptr = format!("{:018p}", l);
/// assert_eq!(l_ptr.len(), 18);
/// assert_eq!(&l_ptr[..2], "0x");
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub trait Pointer {
Expand Down Expand Up @@ -925,7 +935,15 @@ pub trait Pointer {
///
/// let l = Length(100);
///
/// println!("l in scientific notation is: {:e}", l);
/// assert_eq!(
/// format!("l in scientific notation is: {:e}", l),
/// "l in scientific notation is: 1e2"
/// );
///
/// assert_eq!(
/// format!("l in scientific notation is: {:05e}", l),
/// "l in scientific notation is: 001e2"
/// );
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub trait LowerExp {
Expand Down Expand Up @@ -968,7 +986,15 @@ pub trait LowerExp {
///
/// let l = Length(100);
///
/// println!("l in scientific notation is: {:E}", l);
/// assert_eq!(
/// format!("l in scientific notation is: {:E}", l),
/// "l in scientific notation is: 1E2"
/// );
///
/// assert_eq!(
/// format!("l in scientific notation is: {:05E}", l),
/// "l in scientific notation is: 001E2"
/// );
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub trait UpperExp {
Expand Down Expand Up @@ -1813,8 +1839,7 @@ impl<'a> Formatter<'a> {
/// }
/// }
///
/// // prints "[10, 11]"
/// println!("{:?}", Foo(vec![10, 11]));
/// assert_eq!(format!("{:?}", Foo(vec![10, 11])), "[10, 11]");
/// ```
#[stable(feature = "debug_builders", since = "1.2.0")]
pub fn debug_list<'b>(&'b mut self) -> DebugList<'b, 'a> {
Expand All @@ -1837,8 +1862,7 @@ impl<'a> Formatter<'a> {
/// }
/// }
///
/// // prints "{10, 11}"
/// println!("{:?}", Foo(vec![10, 11]));
/// assert_eq!(format!("{:?}", Foo(vec![10, 11])), "{10, 11}");
/// ```
///
/// [`format_args!`]: ../../std/macro.format_args.html
Expand Down Expand Up @@ -1896,8 +1920,10 @@ impl<'a> Formatter<'a> {
/// }
/// }
///
/// // prints "{"A": 10, "B": 11}"
/// println!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)]));
/// assert_eq!(
/// format!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)])),
/// r#"{"A": 10, "B": 11}"#
/// );
/// ```
#[stable(feature = "debug_builders", since = "1.2.0")]
pub fn debug_map<'b>(&'b mut self) -> DebugMap<'b, 'a> {
Expand Down

0 comments on commit a9d6889

Please sign in to comment.