To convert a ethereum_types::H256 to String in Rust

冷暖自知 提交于 2019-12-02 01:35:03

问题


when I tried to convert ethereum_types::H256 to String by using to_string()

use ethereum_types::H256;

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

I expect output to be

"0x0000000000000000000000000000000000000000000000000000000000000000" 

but output is

"0x0000…0000"

回答1:


This (weird) behaviour comes from the fixed-hash crate.

It implements several formatting traits:

  • Display which always elides the middle of the hash.
  • Debug which is equivalent to LowerHex alternate mode.
  • LowerHex and UpperHex which never elide .

Therefore, to get the output you want, use LowerHex with alternate mode:

    println!("{:#x}", s);

(alternatively you can use Debug, but the output of Debug should generally not be relied upon)



来源:https://stackoverflow.com/questions/57350082/to-convert-a-ethereum-typesh256-to-string-in-rust

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!