# rust-WebAssembly **Repository Path**: createmaker/rust-WebAssembly ## Basic Information - **Project Name**: rust-WebAssembly - **Description**: rust-WebAssembly 5 10 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-11-16 - **Last Updated**: 2025-11-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Rust WebAssembly 项目示例 这是一个使用 Rust 和 WebAssembly 构建的示例项目。 ## 功能特性 - ✅ 基本的 Rust 函数导出到 JavaScript - ✅ 字符串处理函数 - ✅ 数学计算函数(加法、斐波那契数列) - ✅ 面向对象的 Calculator 类示例 - ✅ 控制台日志输出 ## 前置要求 - [Rust](https://www.rust-lang.org/tools/install) (最新稳定版) - [wasm-pack](https://rustwasm.github.io/wasm-pack/installer/) - [Node.js](https://nodejs.org/) (可选,用于运行示例) ## 安装 wasm-pack ```bash curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh ``` 或者使用 cargo: ```bash cargo install wasm-pack ``` ## 构建项目 ### 构建为 Web 目标 ```bash wasm-pack build --target web ``` 或者使用 npm 脚本: ```bash npm run build ``` ### 构建为其他目标 ```bash # Node.js npm run build:node # Bundler (webpack, rollup, etc.) npm run build:bundler # 所有目标 npm run build:all ``` 构建完成后,WebAssembly 模块将生成在 `pkg/` 目录中。 ## 使用示例 ### 在 HTML 中使用 查看 `index.html` 文件以了解如何在浏览器中使用 WebAssembly 模块。 ### 基本用法 ```javascript import init, { greet, add, fibonacci, reverse_string, Calculator } from './pkg/rust_webassembly.js'; async function run() { // 初始化 WebAssembly 模块 await init(); // 调用 greet 函数 greet("World"); // 使用计算函数 console.log("2 + 3 =", add(2, 3)); console.log("Fibonacci(10) =", fibonacci(10)); console.log("Reverse 'Hello' =", reverse_string("Hello")); // 使用 Calculator 类 const calc = new Calculator(); calc.add(10); calc.multiply(2); console.log("Calculator value:", calc.get_value()); } run(); ``` ## 项目结构 ``` rust-WebAssembly/ ├── Cargo.toml # Rust 项目配置 ├── package.json # npm 包配置 ├── src/ │ └── lib.rs # 主要的 Rust 代码 ├── pkg/ # 构建输出目录(构建后生成) ├── index.html # 示例 HTML 文件 └── README.md # 项目说明 ``` ## 开发 1. 修改 `src/lib.rs` 中的代码 2. 运行 `wasm-pack build --target web` 重新构建 3. 在浏览器中打开 `index.html` 查看结果 ## 测试 ```bash wasm-pack test --headless --firefox ``` ## 许可证 MIT