@@ -267,11 +267,16 @@ impl<'a> Display for Arguments<'a> {
267
267
}
268
268
}
269
269
270
- /// Format trait for the `?` character. Useful for debugging, all types
271
- /// should implement this.
270
+ /// Format trait for the `?` character.
271
+ ///
272
+ /// `Debug` should format the output in a programmer-facing, debugging context.
272
273
///
273
274
/// Generally speaking, you should just `derive` a `Debug` implementation.
274
275
///
276
+ /// For more information on formatters, see [the module-level documentation][module].
277
+ ///
278
+ /// [module]: ../index.html
279
+ ///
275
280
/// # Examples
276
281
///
277
282
/// Deriving an implementation:
@@ -327,8 +332,39 @@ pub trait Debug {
327
332
fn fmt ( & self , & mut Formatter ) -> Result ;
328
333
}
329
334
330
- /// When a value can be semantically expressed as a String, this trait may be
331
- /// used. It corresponds to the default format, `{}`.
335
+ /// Format trait for an empty format, `{}`.
336
+ ///
337
+ /// `Display` is similar to [`Debug`][debug], but `Display` is for user-facing
338
+ /// output, and so cannot be derived.
339
+ ///
340
+ /// [debug]: trait.Debug.html
341
+ ///
342
+ /// For more information on formatters, see [the module-level documentation][module].
343
+ ///
344
+ /// [module]: ../index.html
345
+ ///
346
+ /// # Examples
347
+ ///
348
+ /// Implementing `Display` on a type:
349
+ ///
350
+ /// ```
351
+ /// use std::fmt;
352
+ ///
353
+ /// struct Point {
354
+ /// x: i32,
355
+ /// y: i32,
356
+ /// }
357
+ ///
358
+ /// impl fmt::Display for Point {
359
+ /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
360
+ /// write!(f, "({}, {})", self.x, self.y)
361
+ /// }
362
+ /// }
363
+ ///
364
+ /// let origin = Point { x: 0, y: 0 };
365
+ ///
366
+ /// println!("The origin is: {}", origin);
367
+ /// ```
332
368
#[ rustc_on_unimplemented = "`{Self}` cannot be formatted with the default \
333
369
formatter; try using `:?` instead if you are using \
334
370
a format string"]
@@ -339,55 +375,308 @@ pub trait Display {
339
375
fn fmt ( & self , & mut Formatter ) -> Result ;
340
376
}
341
377
342
- /// Format trait for the `o` character
378
+ /// Format trait for the `o` character.
379
+ ///
380
+ /// The `Octal` trait should format its output as a number in base-8.
381
+ ///
382
+ /// For more information on formatters, see [the module-level documentation][module].
383
+ ///
384
+ /// [module]: ../index.html
385
+ ///
386
+ /// # Examples
387
+ ///
388
+ /// Basic usage with `i32`:
389
+ ///
390
+ /// ```
391
+ /// let x = 42; // 42 is '52' in octal
392
+ ///
393
+ /// assert_eq!(format!("{:o}", x), "52");
394
+ /// ```
395
+ ///
396
+ /// Implementing `Octal` on a type:
397
+ ///
398
+ /// ```
399
+ /// use std::fmt;
400
+ ///
401
+ /// struct Length(i32);
402
+ ///
403
+ /// impl fmt::Octal for Length {
404
+ /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
405
+ /// let val = self.0;
406
+ ///
407
+ /// write!(f, "{:o}", val) // delegate to i32's implementation
408
+ /// }
409
+ /// }
410
+ ///
411
+ /// let l = Length(9);
412
+ ///
413
+ /// println!("l as octal is: {:o}", l);
414
+ /// ```
343
415
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
344
416
pub trait Octal {
345
417
/// Formats the value using the given formatter.
346
418
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
347
419
fn fmt ( & self , & mut Formatter ) -> Result ;
348
420
}
349
421
350
- /// Format trait for the `b` character
422
+ /// Format trait for the `b` character.
423
+ ///
424
+ /// The `Binary` trait should format its output as a number in binary.
425
+ ///
426
+ /// For more information on formatters, see [the module-level documentation][module].
427
+ ///
428
+ /// [module]: ../index.html
429
+ ///
430
+ /// # Examples
431
+ ///
432
+ /// Basic usage with `i32`:
433
+ ///
434
+ /// ```
435
+ /// let x = 42; // 42 is '101010' in binary
436
+ ///
437
+ /// assert_eq!(format!("{:b}", x), "101010");
438
+ /// ```
439
+ ///
440
+ /// Implementing `Binary` on a type:
441
+ ///
442
+ /// ```
443
+ /// use std::fmt;
444
+ ///
445
+ /// struct Length(i32);
446
+ ///
447
+ /// impl fmt::Binary for Length {
448
+ /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
449
+ /// let val = self.0;
450
+ ///
451
+ /// write!(f, "{:b}", val) // delegate to i32's implementation
452
+ /// }
453
+ /// }
454
+ ///
455
+ /// let l = Length(107);
456
+ ///
457
+ /// println!("l as binary is: {:b}", l);
458
+ /// ```
351
459
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
352
460
pub trait Binary {
353
461
/// Formats the value using the given formatter.
354
462
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
355
463
fn fmt ( & self , & mut Formatter ) -> Result ;
356
464
}
357
465
358
- /// Format trait for the `x` character
466
+ /// Format trait for the `x` character.
467
+ ///
468
+ /// The `LowerHex` trait should format its output as a number in hexidecimal, with `a` through `f`
469
+ /// in lower case.
470
+ ///
471
+ /// For more information on formatters, see [the module-level documentation][module].
472
+ ///
473
+ /// [module]: ../index.html
474
+ ///
475
+ /// # Examples
476
+ ///
477
+ /// Basic usage with `i32`:
478
+ ///
479
+ /// ```
480
+ /// let x = 42; // 42 is '2a' in hex
481
+ ///
482
+ /// assert_eq!(format!("{:x}", x), "2a");
483
+ /// ```
484
+ ///
485
+ /// Implementing `LowerHex` on a type:
486
+ ///
487
+ /// ```
488
+ /// use std::fmt;
489
+ ///
490
+ /// struct Length(i32);
491
+ ///
492
+ /// impl fmt::LowerHex for Length {
493
+ /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
494
+ /// let val = self.0;
495
+ ///
496
+ /// write!(f, "{:x}", val) // delegate to i32's implementation
497
+ /// }
498
+ /// }
499
+ ///
500
+ /// let l = Length(9);
501
+ ///
502
+ /// println!("l as hex is: {:x}", l);
503
+ /// ```
359
504
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
360
505
pub trait LowerHex {
361
506
/// Formats the value using the given formatter.
362
507
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
363
508
fn fmt ( & self , & mut Formatter ) -> Result ;
364
509
}
365
510
366
- /// Format trait for the `X` character
511
+ /// Format trait for the `X` character.
512
+ ///
513
+ /// The `UpperHex` trait should format its output as a number in hexidecimal, with `A` through `F`
514
+ /// in upper case.
515
+ ///
516
+ /// For more information on formatters, see [the module-level documentation][module].
517
+ ///
518
+ /// [module]: ../index.html
519
+ ///
520
+ /// # Examples
521
+ ///
522
+ /// Basic usage with `i32`:
523
+ ///
524
+ /// ```
525
+ /// let x = 42; // 42 is '2A' in hex
526
+ ///
527
+ /// assert_eq!(format!("{:X}", x), "2A");
528
+ /// ```
529
+ ///
530
+ /// Implementing `UpperHex` on a type:
531
+ ///
532
+ /// ```
533
+ /// use std::fmt;
534
+ ///
535
+ /// struct Length(i32);
536
+ ///
537
+ /// impl fmt::UpperHex for Length {
538
+ /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
539
+ /// let val = self.0;
540
+ ///
541
+ /// write!(f, "{:X}", val) // delegate to i32's implementation
542
+ /// }
543
+ /// }
544
+ ///
545
+ /// let l = Length(9);
546
+ ///
547
+ /// println!("l as hex is: {:X}", l);
548
+ /// ```
367
549
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
368
550
pub trait UpperHex {
369
551
/// Formats the value using the given formatter.
370
552
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
371
553
fn fmt ( & self , & mut Formatter ) -> Result ;
372
554
}
373
555
374
- /// Format trait for the `p` character
556
+ /// Format trait for the `p` character.
557
+ ///
558
+ /// The `Pointer` trait should format its output as a memory location. This is commonly presented
559
+ /// as hexidecimal.
560
+ ///
561
+ /// For more information on formatters, see [the module-level documentation][module].
562
+ ///
563
+ /// [module]: ../index.html
564
+ ///
565
+ /// # Examples
566
+ ///
567
+ /// Basic usage with `&i32`:
568
+ ///
569
+ /// ```
570
+ /// let x = &42;
571
+ ///
572
+ /// let address = format!("{:p}", x); // this produces something like '0x7f06092ac6d0'
573
+ /// ```
574
+ ///
575
+ /// Implementing `Pointer` on a type:
576
+ ///
577
+ /// ```
578
+ /// use std::fmt;
579
+ ///
580
+ /// struct Length(i32);
581
+ ///
582
+ /// impl fmt::Pointer for Length {
583
+ /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
584
+ /// // use `as` to convert to a `*const T`, which implements Pointer, which we can use
585
+ ///
586
+ /// write!(f, "{:p}", self as *const Length)
587
+ /// }
588
+ /// }
589
+ ///
590
+ /// let l = Length(42);
591
+ ///
592
+ /// println!("l is in memory here: {:p}", l);
593
+ /// ```
375
594
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
376
595
pub trait Pointer {
377
596
/// Formats the value using the given formatter.
378
597
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
379
598
fn fmt ( & self , & mut Formatter ) -> Result ;
380
599
}
381
600
382
- /// Format trait for the `e` character
601
+ /// Format trait for the `e` character.
602
+ ///
603
+ /// The `LowerExp` trait should format its output in scientific notation with a lower-case `e`.
604
+ ///
605
+ /// For more information on formatters, see [the module-level documentation][module].
606
+ ///
607
+ /// [module]: ../index.html
608
+ ///
609
+ /// # Examples
610
+ ///
611
+ /// Basic usage with `i32`:
612
+ ///
613
+ /// ```
614
+ /// let x = 42.0; // 42.0 is '4.2e1' in scientific notation
615
+ ///
616
+ /// assert_eq!(format!("{:e}", x), "4.2e1");
617
+ /// ```
618
+ ///
619
+ /// Implementing `LowerExp` on a type:
620
+ ///
621
+ /// ```
622
+ /// use std::fmt;
623
+ ///
624
+ /// struct Length(i32);
625
+ ///
626
+ /// impl fmt::LowerExp for Length {
627
+ /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
628
+ /// let val = self.0;
629
+ /// write!(f, "{}e1", val / 10)
630
+ /// }
631
+ /// }
632
+ ///
633
+ /// let l = Length(100);
634
+ ///
635
+ /// println!("l in scientific notation is: {:e}", l);
636
+ /// ```
383
637
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
384
638
pub trait LowerExp {
385
639
/// Formats the value using the given formatter.
386
640
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
387
641
fn fmt ( & self , & mut Formatter ) -> Result ;
388
642
}
389
643
390
- /// Format trait for the `E` character
644
+ /// Format trait for the `E` character.
645
+ ///
646
+ /// The `UpperExp` trait should format its output in scientific notation with an upper-case `E`.
647
+ ///
648
+ /// For more information on formatters, see [the module-level documentation][module].
649
+ ///
650
+ /// [module]: ../index.html
651
+ ///
652
+ /// # Examples
653
+ ///
654
+ /// Basic usage with `f32`:
655
+ ///
656
+ /// ```
657
+ /// let x = 42.0; // 42.0 is '4.2E1' in scientific notation
658
+ ///
659
+ /// assert_eq!(format!("{:E}", x), "4.2E1");
660
+ /// ```
661
+ ///
662
+ /// Implementing `UpperExp` on a type:
663
+ ///
664
+ /// ```
665
+ /// use std::fmt;
666
+ ///
667
+ /// struct Length(i32);
668
+ ///
669
+ /// impl fmt::UpperExp for Length {
670
+ /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
671
+ /// let val = self.0;
672
+ /// write!(f, "{}E1", val / 10)
673
+ /// }
674
+ /// }
675
+ ///
676
+ /// let l = Length(100);
677
+ ///
678
+ /// println!("l in scientific notation is: {:E}", l);
679
+ /// ```
391
680
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
392
681
pub trait UpperExp {
393
682
/// Formats the value using the given formatter.
0 commit comments