本文将介绍一个使用 Rust wasm-bindgen的 async/await 特性来实现异步编程的示例,示例中使用了 wasm-bindgen
将 Rust 代码编译成了 WebAssembly 模块,并在 Node.js 中运行。以下是示例的具体步骤:
在 Cargo.toml
中添加必要的依赖,包括 js-sys
、spin_sleep
、wasm-bindgen
、wasm-bindgen-futures
、futures-core
和 futures-channel-preview
:
[package]
name = "wasm"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
js-sys = "0.3.61"
spin_sleep = "1.1.1"
wasm-bindgen = "0.2.84"
wasm-bindgen-futures = "0.4"
futures-core = { version = '0.3', default-features = false, optional = true }
futures-channel-preview = "0.3.0-al