Struct std::fmt::DebugStruct1.2.0[][src]

#[must_use = "must eventually call `finish()` on Debug builders"]
pub struct DebugStruct<'a, 'b> where
    'b: 'a, 
{ /* fields omitted */ }
Expand description

一个有助于 fmt::Debug 实现的结构体。

当您希望将格式化的结构体作为 Debug::fmt 实现的一部分输出时,此功能很有用。

这可以通过 Formatter::debug_struct 方法创建。

Examples

use std::fmt;

struct Foo {
    bar: i32,
    baz: String,
}

impl fmt::Debug for Foo {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.debug_struct("Foo")
           .field("bar", &self.bar)
           .field("baz", &self.baz)
           .finish()
    }
}

assert_eq!(
    format!("{:?}", Foo { bar: 10, baz: "Hello World".to_string() }),
    "Foo { bar: 10, baz: \"Hello World\" }",
);
Run

Implementations

在生成的结构体输出中添加一个新字段。

Examples

use std::fmt;

struct Bar {
    bar: i32,
    another: String,
}

impl fmt::Debug for Bar {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct("Bar")
           .field("bar", &self.bar) // 我们添加 `bar` 字段。
           .field("another", &self.another) // 我们添加 `another` 字段。
           // 我们甚至添加了一个不存在的字段 (因为为什么不呢? )。
           .field("not_existing_field", &1)
           .finish() // 我们很高兴去!
    }
}

assert_eq!(
    format!("{:?}", Bar { bar: 10, another: "Hello World".to_string() }),
    "Bar { bar: 10, another: \"Hello World\", not_existing_field: 1 }",
);
Run

将结构体标记为非穷举,向 reader 指示在调试表示中未显示其他一些字段。

Examples

use std::fmt;

struct Bar {
    bar: i32,
    hidden: f32,
}

impl fmt::Debug for Bar {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct("Bar")
           .field("bar", &self.bar)
           .finish_non_exhaustive() // 证明存在其他 field(s)。
    }
}

assert_eq!(
    format!("{:?}", Bar { bar: 10, hidden: 1.0 }),
    "Bar { bar: 10, .. }",
);
Run

完成输出并返回遇到的任何错误。

Examples

use std::fmt;

struct Bar {
    bar: i32,
    baz: String,
}

impl fmt::Debug for Bar {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct("Bar")
           .field("bar", &self.bar)
           .field("baz", &self.baz)
           .finish() // 您需要调用它到 "finish"
                     // 结构体格式化。
    }
}

assert_eq!(
    format!("{:?}", Bar { bar: 10, baz: "Hello World".to_string() }),
    "Bar { bar: 10, baz: \"Hello World\" }",
);
Run

Auto Trait Implementations

Blanket Implementations

获取 selfTypeIdRead more

从拥有的值中一成不变地借用。 Read more

从拥有的值中借用。 Read more

执行转换。

执行转换。

发生转换错误时返回的类型。

执行转换。

发生转换错误时返回的类型。

执行转换。