Trait core::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,
)");
RunImplementors
1.29.0[src]
impl<I, U> Debug for Flatten<I> where
I: Debug + Iterator<Item: IntoIterator<IntoIter = U, Item = U::Item>>,
U: Debug + Iterator,
1.4.0[src]
impl<Ret, A, B, C, D, E> Debug for extern "C" fn(_: A, _: B, _: C, _: D, _: E, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E> Debug for unsafe extern "C" fn(_: A, _: B, _: C, _: D, _: E) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E> Debug for unsafe extern "C" fn(_: A, _: B, _: C, _: D, _: E, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F> Debug for extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F> Debug for extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F> Debug for unsafe fn(_: A, _: B, _: C, _: D, _: E, _: F) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F> Debug for unsafe extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F> Debug for unsafe extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G> Debug for fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G> Debug for extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G> Debug for extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G> Debug for unsafe fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G> Debug for unsafe extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G> Debug for unsafe extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H> Debug for fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H> Debug for extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H> Debug for extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H> Debug for unsafe fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H> Debug for unsafe extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H> Debug for unsafe extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I> Debug for fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I> Debug for extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I> Debug for extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I> Debug for unsafe fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I> Debug for unsafe extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I> Debug for unsafe extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J> Debug for fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J> Debug for extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J> Debug for extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J> Debug for unsafe fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J> Debug for unsafe extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J> Debug for unsafe extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K> Debug for fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K> Debug for extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K> Debug for extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K> Debug for unsafe fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K> Debug for unsafe extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K> Debug for unsafe extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Debug for fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Debug for extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Debug for extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L, ...) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Debug for unsafe fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> Ret
1.4.0[src]
impl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Debug for unsafe extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> Ret
1.4.0[src]