rust - 如何调试打印 NEAR 协议集合?
问题描述
我很难漂亮地打印 NEAR 协议集合。我相信最好的方法是为Map、Set和Vector实现调试。这是我认为我应该做的事情:
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!
打印出这些集合的内容?
解决方案
我相信您采取的方法与您的目标不同。据我了解,您希望在学习如何使用这些集合时将其打印出来。以下是您提到的三个系列的示例。使用每个集合,.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
推荐阅读
- git - SSH 与 Git 克隆
- python - 尝试创建一个执行简单统计计算的类,ValueError: invalid literal for int() with base 10: ''
- azure - SharePoint Online CData JDBC Databricks AD 已验证调用获取 HTTP 协议错误。500 服务器错误
- sql - AWS Timestream 查询不同的测量值
- node.js - "gulp serve" 将返回此错误 "ReferenceError: primordials is not defined"
- java - 如何判断列表是否已经加载?
- arrays - 为什么在一个单元中只考虑数组的第一个元素?
- swift - Xcode:在框架模块中包含非模块化标头
- orm - 有没有人将对象图存储为单个文件而不是使用 ORM?
- tensorflow - 在不同机器上保存和加载 Universal Sentence Encoder 模型