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"
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 toLowerHex
alternate mode.LowerHex
andUpperHex
which never elide .Therefore, to get the output you want, use
LowerHex
with alternate mode:(alternatively you can use
Debug
, but the output ofDebug
should generally not be relied upon)