首页 > 解决方案 > 如何调试打印 NEAR 协议集合?

问题描述

我很难漂亮地打印 NEAR 协议集合。我相信最好的方法是为MapSetVector实现调试。这是我认为我应该做的事情:

 use std::fmt;    
 impl fmt::Debug for Map {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 
      // How do I fill this out?
    } 
 }

https://docs.rs/near-sdk/0.10.0/near_sdk/collections/index.html

如果这是错误的方法,我该如何println!打印出这些集合的内容?

标签: rustnearprotocol

解决方案


我相信您采取的方法与您的目标不同。据我了解,您希望在学习如何使用这些集合时将其打印出来。以下是您提到的三个系列的示例。使用每个集合,.to_vec()您可以在运行测试时很好地看到结果。

use near_sdk::{collections::Map, collections::Vector, collections::Set};

…

// you can place this inside a test

let mut my_near_vector: Vector<String> = Vector::new(b"something".to_vec());
my_near_vector.push(&"aloha".to_string());
my_near_vector.push(&"honua".to_string());
println!("Vector {:?}", my_near_vector.to_vec());

let mut my_near_map: Map<String, String> = Map::new(b"it's a dictionary".to_vec());
my_near_map.insert(&"aardvark".to_string(), &"a nocturnal burrowing mammal with long ears".to_string());
my_near_map.insert(&"beelzebub".to_string(), &"a fallen angel in Milton's Paradise Lost".to_string());
println!("Map {:?}", my_near_map.to_vec());

let mut my_near_set: Set<String> = Set::new(b"phonetic alphabet".to_vec());
my_near_set.insert(&"alpha".to_string());
my_near_set.insert(&"bravo".to_string());
println!("Set {:?}", my_near_set.to_vec());

如果您随后cargo test -- --nocapture在项目中运行,您将看到如下输出:

running 1 test
Vector ["aloha", "honua"]
Map [("aardvark", "a nocturnal burrowing mammal with long ears"), ("beelzebub", "a fallen angel in Milton\'s Paradise Lost")]
Set ["alpha", "bravo"]
test tests::demo ... ok

推荐阅读