当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Rust DebugMap.entry用法及代码示例


本文简要介绍rust语言中 core::fmt::DebugMap.entry 的用法。

用法

pub fn entry(&mut self, key: &dyn Debug, value: &dyn Debug) -> &mut Self

向Map输出添加新条目。

例子

use std::fmt;

struct Foo(Vec<(String, i32)>);

impl fmt::Debug for Foo {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_map()
           .entry(&"whole", &self.0) // We add the "whole" entry.
           .finish()
    }
}

assert_eq!(
    format!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)])),
    "{\"whole\": [(\"A\", 10), (\"B\", 11)]}",
);

相关用法


注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 core::fmt::DebugMap.entry。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。