Trait rustfmt_nightly::fmt::Debug 1.0.0[−][src]
Expand description
? formatting.
Debug should format the output in a programmer-facing, debugging context.
Generally speaking, you should just derive a Debug implementation.
When used with the alternate format specifier #?, the output is pretty-printed.
For more information on formatters, see the module-level documentation.
This trait can be used with #[derive] if all fields implement Debug. When
derived for structs, it will use the name of the struct, then {, then a
comma-separated list of each field’s name and Debug value, then }. For
enums, it will use the name of the variant and, if applicable, (, then the
Debug values of the fields, then ).
Stability
Derived Debug formats are not stable, and so may change with future Rust
versions. Additionally, Debug implementations of types provided by the
standard library (libstd, libcore, liballoc, etc.) are not stable, and
may also change with future Rust versions.
Examples
Deriving an implementation:
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
let origin = Point { x: 0, y: 0 };
assert_eq!(format!("The origin is: {:?}", origin), "The origin is: Point { x: 0, y: 0 }");Manually implementing:
use std::fmt;
struct Point {
x: i32,
y: i32,
}
impl fmt::Debug for Point {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Point")
.field("x", &self.x)
.field("y", &self.y)
.finish()
}
}
let origin = Point { x: 0, y: 0 };
assert_eq!(format!("The origin is: {:?}", origin), "The origin is: Point { x: 0, y: 0 }");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: {:#?}.
Pretty-printing with #?:
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
let origin = Point { x: 0, y: 0 };
assert_eq!(format!("The origin is: {:#?}", origin),
"The origin is: Point {
x: 0,
y: 0,
}");Required methods
Formats the value using the given formatter.
Examples
use std::fmt;
struct Position {
longitude: f32,
latitude: f32,
}
impl fmt::Debug for Position {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("")
.field(&self.longitude)
.field(&self.latitude)
.finish()
}
}
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,
)");Implementations on Foreign Types
1.16.0[src]impl<'_, T, S> Debug for SymmetricDifference<'_, T, S> where
T: Debug + Eq + Hash,
S: BuildHasher,
impl<'_, T, S> Debug for SymmetricDifference<'_, T, S> where
T: Debug + Eq + Hash,
S: BuildHasher,
1.16.0[src]impl<'_, T, S> Debug for Difference<'_, T, S> where
T: Debug + Eq + Hash,
S: BuildHasher,
impl<'_, T, S> Debug for Difference<'_, T, S> where
T: Debug + Eq + Hash,
S: BuildHasher,
1.16.0[src]impl<'_, T, S> Debug for Intersection<'_, T, S> where
T: Debug + Eq + Hash,
S: BuildHasher,
impl<'_, T, S> Debug for Intersection<'_, T, S> where
T: Debug + Eq + Hash,
S: BuildHasher,
1.9.0[src]impl<I, U, F> Debug for FlatMap<I, U, F> where
I: Debug,
U: IntoIterator,
<U as IntoIterator>::IntoIter: Debug,
impl<I, U, F> Debug for FlatMap<I, U, F> where
I: Debug,
U: IntoIterator,
<U as IntoIterator>::IntoIter: Debug,
impl<T, const LANES: usize> Debug for Mask<T, LANES> where
T: MaskElement + Debug,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> Debug for Mask<T, LANES> where
T: MaskElement + Debug,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> Debug for Simd<T, LANES> where
T: SimdElement + Debug,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> Debug for Simd<T, LANES> where
T: SimdElement + Debug,
LaneCount<LANES>: SupportedLaneCount,
Prints token tree in a form convenient for debugging.
Prints token in a form convenient for debugging.
Prints a span in a form convenient for debugging.
impl<I, T> Debug for TupleWindows<I, T> where
I: Debug + Iterator<Item = <T as TupleCollect>::Item>,
T: Debug + HomogeneousTuple,
impl<I, T> Debug for TupleWindows<I, T> where
I: Debug + Iterator<Item = <T as TupleCollect>::Item>,
T: Debug + HomogeneousTuple,
impl<T> Debug for TupleBuffer<T> where
T: Debug + HomogeneousTuple,
<T as TupleCollect>::Buffer: Debug,
impl<T> Debug for TupleBuffer<T> where
T: Debug + HomogeneousTuple,
<T as TupleCollect>::Buffer: Debug,
impl<I, T> Debug for TupleCombinations<I, T> where
I: Debug + Iterator,
T: Debug + HasCombination<I>,
<T as HasCombination<I>>::Combination: Debug,
impl<I, T> Debug for TupleCombinations<I, T> where
I: Debug + Iterator,
T: Debug + HasCombination<I>,
<T as HasCombination<I>>::Combination: Debug,
impl<'c, 't> Debug for SubCaptureMatches<'c, 't>
impl<'c, 't> Debug for SubCaptureMatches<'c, 't>
impl Debug for Regex
impl Debug for Regex
impl<'r, 't> Debug for CaptureMatches<'r, 't>
impl<'r, 't> Debug for CaptureMatches<'r, 't>
impl<'r, 't> Debug for CaptureMatches<'r, 't>
impl<'r, 't> Debug for CaptureMatches<'r, 't>
impl<'c, 't> Debug for SubCaptureMatches<'c, 't>
impl<'c, 't> Debug for SubCaptureMatches<'c, 't>
impl Debug for Regex
impl Debug for Regex
impl<'a> Debug for ClassUnicodeIter<'a>
impl<'a> Debug for ClassUnicodeIter<'a>
impl<'s, 'h> Debug for FindIter<'s, 'h>
impl<'s, 'h> Debug for FindIter<'s, 'h>
impl<'h, 'n> Debug for FindIter<'h, 'n>
impl<'h, 'n> Debug for FindIter<'h, 'n>
impl<'h, 'n> Debug for FindRevIter<'h, 'n>
impl<'h, 'n> Debug for FindRevIter<'h, 'n>
impl<'a> Debug for SourceAnnotation<'a>
impl<'a> Debug for SourceAnnotation<'a>
impl<'a> Debug for DisplayTextFragment<'a>
impl<'a> Debug for DisplayTextFragment<'a>
impl<'a> Debug for DisplaySourceLine<'a>
impl<'a> Debug for DisplaySourceLine<'a>
impl<'a> Debug for WordsWithBreakIndices<'a>
impl<'a> Debug for WordsWithBreakIndices<'a>
impl<'scope, 'env> Debug for ScopedThreadBuilder<'scope, 'env>
impl<'scope, 'env> Debug for ScopedThreadBuilder<'scope, 'env>
impl<'_, T> Debug for ScopedJoinHandle<'_, T>
impl<'_, T> Debug for ScopedJoinHandle<'_, T>
impl Debug for DiagnosticSpanMacroExpansion
impl Debug for DiagnosticSpanMacroExpansion
impl<'a> Debug for Utf8PrefixComponent<'a>
impl<'a> Debug for Utf8PrefixComponent<'a>
impl Debug for Style
impl Debug for Style
Styles have a special Debug implementation that only shows the fields that
are set. Fields that haven’t been touched aren’t included in the output.
This behaviour gets bypassed when using the alternate formatting mode
format!("{:#?}").
use ansi_term::Colour::{Red, Blue};
assert_eq!("Style { fg(Red), on(Blue), bold, italic }",
format!("{:?}", Red.on(Blue).bold().italic()));