Shashi Raz
Shashi Raz

Reputation: 71

To convert a ethereum_types::H256 to String in Rust

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"

Upvotes: 7

Views: 2682

Answers (1)

mcarton
mcarton

Reputation: 30021

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)

Upvotes: 6

Related Questions