To convert a ethereum_types::H256 to String in Rus

2020-04-14 12:12发布

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条回答
祖国的老花朵
2楼-- · 2020-04-14 12:24

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

It implements several formatting traits:

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)

查看更多
登录 后发表回答