0013-wasm-hello world
环境
- Time 2022-05-12
- Rust 1.60.0
- Node 12.22.5
- wasm-pack 0.10.2
前言
说明
环境配置参考:https://rustwasm.github.io/docs/book/game-of-life/setup.html
参考:https://rustwasm.github.io/docs/book/game-of-life/hello-world.html
目标
实现第一个 wasm 程序,使用 wasm 弹出一个 alert 框。
Cargo.toml
[package]
edition = "2021"
name = "game"
version = "0.1.0"
[lib]
crate-type = ["cdylib", "rlib"]
[features]
default = ["console_error_panic_hook"]
[dependencies]
console_error_panic_hook = {version = "*", optional = true}
wasm-bindgen = "*"
wee_alloc = {version = "*", optional = true}
[dev-dependencies]
wasm-bindgen-test = "*"
[profile.release]
opt-level = "s"
lib.rs
mod utils;
use wasm_bindgen::prelude::*;
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet() {
alert("hello wasm");
}
utils.rs
pub fn set_panic_hook() {
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}
增加构建目标
rustup target add wasm32-unknown-unknown
wasm-pack
wasm-pack build
[INFO]: Checking for the Wasm target...
[INFO]: Compiling to Wasm...
warning: function is never used: `set_panic_hook`
--> src\utils.rs:1:8
|
1 | pub fn set_panic_hook() {
| ^^^^^^^^^^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: `game` (lib) generated 1 warning
Finished release [optimized] target(s) in 0.07s
[WARN]: :-) origin crate has no README
[INFO]: Installing wasm-bindgen...
[INFO]: Optimizing wasm binaries with `wasm-opt`...
[INFO]: Optional fields missing from Cargo.toml: 'description', 'repository', and 'license'. These are not necessary, but recommended
[INFO]: :-) Done in 9.67s
[INFO]: :-) Your wasm pkg is ready to publish at C:\Users\jiangbo\work\game\pkg.
生成前端
npm init wasm-app www
增加依赖
"dependencies": {
"game": "file:../pkg"
}
启动
npm run start
总结
实现了第一个 wasm 程序,在浏览器中弹出了一个 alert 框。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!