Skip to content

Commit 3e62e20

Browse files
committed
Rollup merge of #26828 - steveklabnik:gh25765, r=bluss
Each formatting trait now has an example of implementation, as well as a fuller description of what it's supposed to output. It also contains a link to the module-level documentation which Fixes #25765
2 parents 015441b + 3a6cd64 commit 3e62e20

File tree

1 file changed

+300
-11
lines changed

1 file changed

+300
-11
lines changed

src/libcore/fmt/mod.rs

+300-11
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,16 @@ impl<'a> Display for Arguments<'a> {
267267
}
268268
}
269269

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.
272273
///
273274
/// Generally speaking, you should just `derive` a `Debug` implementation.
274275
///
276+
/// For more information on formatters, see [the module-level documentation][module].
277+
///
278+
/// [module]: ../index.html
279+
///
275280
/// # Examples
276281
///
277282
/// Deriving an implementation:
@@ -327,8 +332,39 @@ pub trait Debug {
327332
fn fmt(&self, &mut Formatter) -> Result;
328333
}
329334

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+
/// ```
332368
#[rustc_on_unimplemented = "`{Self}` cannot be formatted with the default \
333369
formatter; try using `:?` instead if you are using \
334370
a format string"]
@@ -339,55 +375,308 @@ pub trait Display {
339375
fn fmt(&self, &mut Formatter) -> Result;
340376
}
341377

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+
/// ```
343415
#[stable(feature = "rust1", since = "1.0.0")]
344416
pub trait Octal {
345417
/// Formats the value using the given formatter.
346418
#[stable(feature = "rust1", since = "1.0.0")]
347419
fn fmt(&self, &mut Formatter) -> Result;
348420
}
349421

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+
/// ```
351459
#[stable(feature = "rust1", since = "1.0.0")]
352460
pub trait Binary {
353461
/// Formats the value using the given formatter.
354462
#[stable(feature = "rust1", since = "1.0.0")]
355463
fn fmt(&self, &mut Formatter) -> Result;
356464
}
357465

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+
/// ```
359504
#[stable(feature = "rust1", since = "1.0.0")]
360505
pub trait LowerHex {
361506
/// Formats the value using the given formatter.
362507
#[stable(feature = "rust1", since = "1.0.0")]
363508
fn fmt(&self, &mut Formatter) -> Result;
364509
}
365510

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+
/// ```
367549
#[stable(feature = "rust1", since = "1.0.0")]
368550
pub trait UpperHex {
369551
/// Formats the value using the given formatter.
370552
#[stable(feature = "rust1", since = "1.0.0")]
371553
fn fmt(&self, &mut Formatter) -> Result;
372554
}
373555

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+
/// ```
375594
#[stable(feature = "rust1", since = "1.0.0")]
376595
pub trait Pointer {
377596
/// Formats the value using the given formatter.
378597
#[stable(feature = "rust1", since = "1.0.0")]
379598
fn fmt(&self, &mut Formatter) -> Result;
380599
}
381600

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+
/// ```
383637
#[stable(feature = "rust1", since = "1.0.0")]
384638
pub trait LowerExp {
385639
/// Formats the value using the given formatter.
386640
#[stable(feature = "rust1", since = "1.0.0")]
387641
fn fmt(&self, &mut Formatter) -> Result;
388642
}
389643

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+
/// ```
391680
#[stable(feature = "rust1", since = "1.0.0")]
392681
pub trait UpperExp {
393682
/// Formats the value using the given formatter.

0 commit comments

Comments
 (0)