當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Rust DebugMap.entries用法及代碼示例


本文簡要介紹rust語言中 std::fmt::DebugMap.entries 的用法。

用法

pub fn entries<K, V, I>(&mut self, entries: I) -> &mut DebugMap<'a, 'b> where    K: Debug,    V: Debug,    I: IntoIterator<Item = (K, V)>,

將條目迭代器的內容添加到映射輸出。

例子

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()
           // We map our vec so each entries' first field will become
           // the "key".
           .entries(self.0.iter().map(|&(ref k, ref v)| (k, v)))
           .finish()
    }
}

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

相關用法


注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 std::fmt::DebugMap.entries。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。