如何将数字传递给 web3.eth().block()?

How do I pass in a number to web3.eth().block()?

我正在使用 rust-web3 crate to connect to an ethereum node and get information about a block by it's block number (or block height). Based on this example 下面是我如何实现它的:

use web3;
use web3::types::{BlockId, BlockNumber, U64};

#[tokio::main]
async fn main() -> web3::Result<()> {
    let transport = web3::transports::Http::new(WEB3_URL)?;
    let web3 = web3::Web3::new(transport);

    web3.eth().block(BlockId::Number(BlockNumber::Number(U64([42]))));
    Ok(())
}

是否有更简单的方法将 42 传递给 web3.eth().block()

现在我正在使用两个枚举和 U64 结构来生成编译器接受的变量。我是生锈的新手,我想我错过了一个可以简化它的重要语言概念。

有一个impl From<U64> for BlockId(source),所以你可以稍微缩短调用:

web3.eth().block(U64([42]).into());