将 Rust 中的 ethereum_types::H256 类型转换为字符串

6

当我尝试使用to_string()将ethereum_types::H256转换为String时,出现了问题。

use ethereum_types::H256;

fn main() {   
    let s = H256::zero();
    println!("{}", s);
}

我期望的输出结果是:
"0x0000000000000000000000000000000000000000000000000000000000000000" 

但输出的结果是

"0x0000…0000"
1个回答

6

它报错了,因为std::string::String没有实现std::fmt::LowerHex特性。 - Shashi Raz
你是否在哈希上调用了 to_string?请注意,我已经从你的问题中将其编辑掉了,因为使用 format! 宏族列时从不需要调用 to_stringto_string 总是等效于 Display,但调用 to_string 然后将结果传递给 format! 宏会导致效率降低。它还会阻止你使用除 Display 之外的特性。 - mcarton
是的,我已经使用过 to_string。我的最终目标是将 s 转换为字符串并写入 .csv 文件,但是当我这样做时,它会在我的文件中写入 "0x0000…0000" - Shashi Raz
我建议您阅读fmt模块的文档。您不需要使用to_string - mcarton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接