Trait alloc::fmt::Debug 1.0.0[−][src]
Expand description
?
格式。
Debug
应该在面向程序员的调试上下文中格式化输出。
一般来说,您应该只将 derive
和 Debug
实现。
当与备用格式说明符 #?
一起使用时,输出将被漂亮地打印。
有关格式化程序的更多信息,请参见 模块级文档。
如果所有字段都实现 Debug
,则此 trait 可以与 #[derive]
一起使用。当为结构体 derived' 时,它将使用
struct的名称,然后是
{,然后是每个字段名称和
Debug值的逗号分隔列表,然后是
}。 对于
enum,它将使用变体的名称,如果适用,将使用
(,然后是字段的
Debug值,然后是
)`。
Stability
派生的 Debug
格式不稳定,因此 future Rust 版本可能会更改。此外,标准库提供的类型 (libstd
,libcore
,liballoc
等) 的 Debug
实现不稳定,并且可能随 future Rust 版本而改变。
Examples
派生实现:
#[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 }");
Run手动实现:
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 }");
RunFormatter
结构体上有许多辅助方法可以帮助您实现手动实现,例如 debug_struct
。
不希望使用 Formatter
trait (debug_struct
, debug_tuple
, debut_list
, debug_set
, debug_map
) 提供的标准调试表示套件的类型可以通过手动将任意表示写入 Formatter
来执行完全自定义的操作。
impl fmt::Debug for Point {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Point [{} {}]", self.x, self.y)
}
}
Run使用 derive
或 Formatter
上的调试构建器 API 的 Debug
实现支持使用备用标志 {:#?}
的漂亮打印。
使用 #?
进行漂亮的打印:
#[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,
}");
RunRequired methods
使用给定的格式化程序格式化该值。
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,
)");
RunImplementations on Foreign Types
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,
1.9.0[src]impl<I, U, F> Debug for FlatMap<I, U, F> where
I: Debug,
U: IntoIterator,
<U as IntoIterator>::IntoIter: Debug,
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<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,
Implementors
1.27.0[src]
impl<'_, T, P> Debug for alloc::slice::RSplit<'_, T, P> where
T: Debug,
P: FnMut(&T) -> bool,
1.9.0[src]
impl<'_, T, P> Debug for alloc::slice::RSplitN<'_, T, P> where
T: Debug,
P: FnMut(&T) -> bool,
1.9.0[src]
impl<'_, T, P> Debug for alloc::slice::Split<'_, T, P> where
T: Debug,
P: FnMut(&T) -> bool,
1.51.0[src]
impl<'_, T, P> Debug for alloc::slice::SplitInclusive<'_, T, P> where
T: Debug,
P: FnMut(&T) -> bool,
1.51.0[src]
impl<'_, T, P> Debug for SplitInclusiveMut<'_, T, P> where
T: Debug,
P: FnMut(&T) -> bool,
1.9.0[src]
impl<'_, T, P> Debug for alloc::slice::SplitN<'_, T, P> where
T: Debug,
P: FnMut(&T) -> bool,
1.21.0[src]
impl<'a, I: Debug + Iterator + 'a, A: Debug + Allocator + 'a> Debug for Splice<'a, I, A> where
I::Item: Debug,
1.5.0[src]
impl<'a, P> Debug for MatchIndices<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Debug,
1.2.0[src]
impl<'a, P> Debug for Matches<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Debug,
1.5.0[src]
impl<'a, P> Debug for RMatchIndices<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Debug,
1.2.0[src]
impl<'a, P> Debug for RMatches<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Debug,
impl<'a, P> Debug for alloc::str::RSplit<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Debug,
impl<'a, P> Debug for alloc::str::RSplitN<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Debug,
impl<'a, P> Debug for RSplitTerminator<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Debug,
impl<'a, P> Debug for alloc::str::Split<'a, P> where
P: Pattern<'a>,
<P as Pattern<'a>>::Searcher: Debug,
1.51.0[src]