英文:
To convert a String to H256 in Rust
问题
如何将类似于"0x0000000000000000000000000000000000000000000000000000000000000000"的字符串转换为H256类型?我认为应该有一种简单的方法,就像一行代码那样。
我找到了一些库,但是没有找到简单的解决办法。
英文:
when I get a string like "0x0000000000000000000000000000000000000000000000000000000000000000", how to convert it to H256 type? I thought there is a simple way like a oneline code.
I found some crate, but can`t find a simply way to solve that.
答案1
得分: 1
Just use String.parse()
method.
String.parse()
can parse into any type that implements the FromStr
trait, like Struct ethers::core::types::H256
.
use ethers::core::types::H256;
fn main() {
let a: H256 = "0x0000000000000000000000000000000000000000000000000000000000000000".parse().unwrap();
//using turbofish syntax.
let b = "0x1234567890987654321234566535655565653565666656536566565656656560".parse::<H256>().unwrap();
println!("{}\n{}", a, b);
}
//0x0000…0000
//0x1234…6560
英文:
Just use String.parse()
method.
String.parse()
can parse into any type that implements the FromStr
trait, like Struct ethers::core::types::H256
.
use ethers::core::types::H256;
fn main() {
let a: H256 = "0x0000000000000000000000000000000000000000000000000000000000000000".parse().unwrap();
//using turbofish syntax.
let b = "0x1234567890987654321234566535655565653565666656536566565656656560".parse::<H256>().unwrap();
println!("{}\n{}", a, b);
}
//0x0000…0000
//0x1234…6560
答案2
得分: 0
Rust:
use ckb_types::h256;
h256!("0xf8dd02cc2a3174933fac4e87d7f6360c3cc67167db105b28d0bc434a60674e49")
cargo.toml:
ckb-types = "0.108"
英文:
use Macro ckb_types::h256
Rust :
use ckb_types::h256;
h256!("0xf8dd02cc2a3174933fac4e87d7f6360c3cc67167db105b28d0bc434a60674e49")
cargo.toml:
ckb-types = "0.108"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论