在 Debian 中安装和使用 Rust 编译器的步骤如下:
sudo apt update
sudo apt install rustc cargo
rustc --version
cargo --version
cargo new hello_world
cd hello_world
cargo build
cargo run
sudo apt install wabt
wasm-bindgen
:cargo install wasm-bindgen-cli
cargo new wasm_example
cd wasm_example
Cargo.toml
中添加依赖:[dependencies]
wasm-bindgen = "0.2"
src/lib.rs
中:use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
cargo build --target wasm32-unknown-unknown --release
wasm-bindgen target/wasm32-unknown-unknown/release/wasm_example.wasm --out-dir ./pkg --target web
index.html
文件:<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Rust WebAssembly Example</title>
</head>
<body>
<script type="module">
import init, { add } from './pkg/wasm_example.js';
async function run() {
await init();
const result = add(2, 3);
console.log(`2 + 3 = ${result}`);
}
run();
</script>
</body>
</html>
python3 -m http.server
在浏览器中打开 http://localhost:8000
查看结果。
以上步骤涵盖了在 Debian 上安装 Rust 编译器、使用 Cargo 管理项目、创建和运行一个简单的 Rust WebAssembly 项目的完整流程。