@@ -273,6 +273,8 @@ impl<'a> Display for Arguments<'a> {
273
273
///
274
274
/// Generally speaking, you should just `derive` a `Debug` implementation.
275
275
///
276
+ /// When used with the alternate format specifier `#?`, the output is pretty-printed.
277
+ ///
276
278
/// For more information on formatters, see [the module-level documentation][module].
277
279
///
278
280
/// [module]: ../index.html
@@ -314,13 +316,42 @@ impl<'a> Display for Arguments<'a> {
314
316
/// println!("The origin is: {:?}", origin);
315
317
/// ```
316
318
///
319
+ /// This outputs:
320
+ ///
321
+ /// ```text
322
+ /// The origin is: Point { x: 0, y: 0 }
323
+ /// ```
324
+ ///
317
325
/// There are a number of `debug_*` methods on `Formatter` to help you with manual
318
326
/// implementations, such as [`debug_struct`][debug_struct].
319
327
///
320
328
/// `Debug` implementations using either `derive` or the debug builder API
321
329
/// on `Formatter` support pretty printing using the alternate flag: `{:#?}`.
322
330
///
323
331
/// [debug_struct]: ../std/fmt/struct.Formatter.html#method.debug_struct
332
+ ///
333
+ /// Pretty printing with `#?`:
334
+ ///
335
+ /// ```
336
+ /// #[derive(Debug)]
337
+ /// struct Point {
338
+ /// x: i32,
339
+ /// y: i32,
340
+ /// }
341
+ ///
342
+ /// let origin = Point { x: 0, y: 0 };
343
+ ///
344
+ /// println!("The origin is: {:#?}", origin);
345
+ /// ```
346
+ ///
347
+ /// This outputs:
348
+ ///
349
+ /// ```text
350
+ /// The origin is: Point {
351
+ /// x: 0,
352
+ /// y: 0
353
+ /// }
354
+ /// ```
324
355
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
325
356
#[ rustc_on_unimplemented = "`{Self}` cannot be formatted using `:?`; if it is \
326
357
defined in your crate, add `#[derive(Debug)]` or \
@@ -379,6 +410,8 @@ pub trait Display {
379
410
///
380
411
/// The `Octal` trait should format its output as a number in base-8.
381
412
///
413
+ /// The alternate flag, `#`, adds a `0o` in front of the output.
414
+ ///
382
415
/// For more information on formatters, see [the module-level documentation][module].
383
416
///
384
417
/// [module]: ../index.html
@@ -391,6 +424,7 @@ pub trait Display {
391
424
/// let x = 42; // 42 is '52' in octal
392
425
///
393
426
/// assert_eq!(format!("{:o}", x), "52");
427
+ /// assert_eq!(format!("{:#o}", x), "0o52");
394
428
/// ```
395
429
///
396
430
/// Implementing `Octal` on a type:
@@ -423,6 +457,8 @@ pub trait Octal {
423
457
///
424
458
/// The `Binary` trait should format its output as a number in binary.
425
459
///
460
+ /// The alternate flag, `#`, adds a `0b` in front of the output.
461
+ ///
426
462
/// For more information on formatters, see [the module-level documentation][module].
427
463
///
428
464
/// [module]: ../index.html
@@ -435,6 +471,7 @@ pub trait Octal {
435
471
/// let x = 42; // 42 is '101010' in binary
436
472
///
437
473
/// assert_eq!(format!("{:b}", x), "101010");
474
+ /// assert_eq!(format!("{:#b}", x), "0b101010");
438
475
/// ```
439
476
///
440
477
/// Implementing `Binary` on a type:
@@ -468,6 +505,8 @@ pub trait Binary {
468
505
/// The `LowerHex` trait should format its output as a number in hexidecimal, with `a` through `f`
469
506
/// in lower case.
470
507
///
508
+ /// The alternate flag, `#`, adds a `0x` in front of the output.
509
+ ///
471
510
/// For more information on formatters, see [the module-level documentation][module].
472
511
///
473
512
/// [module]: ../index.html
@@ -480,6 +519,7 @@ pub trait Binary {
480
519
/// let x = 42; // 42 is '2a' in hex
481
520
///
482
521
/// assert_eq!(format!("{:x}", x), "2a");
522
+ /// assert_eq!(format!("{:#x}", x), "0x2a");
483
523
/// ```
484
524
///
485
525
/// Implementing `LowerHex` on a type:
@@ -513,6 +553,8 @@ pub trait LowerHex {
513
553
/// The `UpperHex` trait should format its output as a number in hexidecimal, with `A` through `F`
514
554
/// in upper case.
515
555
///
556
+ /// The alternate flag, `#`, adds a `0x` in front of the output.
557
+ ///
516
558
/// For more information on formatters, see [the module-level documentation][module].
517
559
///
518
560
/// [module]: ../index.html
@@ -525,6 +567,7 @@ pub trait LowerHex {
525
567
/// let x = 42; // 42 is '2A' in hex
526
568
///
527
569
/// assert_eq!(format!("{:X}", x), "2A");
570
+ /// assert_eq!(format!("{:#X}", x), "0x2A");
528
571
/// ```
529
572
///
530
573
/// Implementing `UpperHex` on a type:
0 commit comments