Minimal Rust-Node.js example using wasm-bindgen and TypeScript.
make setup
make build
make test
import { add, Calculator } from 'nodejs-rust-mvp';
// Add two numbers
add(5, 10); // 15
// Use Calculator
const calc = new Calculator(5);
calc.value; // 5
calc.add(10); // 15
To run a simple example that demonstrates the functionality and performance:
make example
Here's a quick demonstration of the development workflow with Rust and WebAssembly integration:
-
First, add the
web-sys
dependency for console logging capabilities:# Via CLI cargo add web-sys --features console
# Or in Cargo.toml # [dependencies] # web-sys = { version = "0.3", features = ["console"] }
-
Add a debug print statement to the Rust code:
diff --git a/src/lib.rs b/src/lib.rs --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ use wasm_bindgen::prelude::*; // Export a simple add function to JavaScript #[wasm_bindgen] pub fn add(a: i32, b: i32) -> i32 { + web_sys::console::log_1(&format!("🦀 FROM RUST: Adding {} + {}", a, b).into()); a + b }
-
Rebuild and run tests with console output visible:
make rebuild; RUST_BACKTRACE=1 yarn test
-
See the Rust console logs in your test output:
======= RUNS ======= ✓ src/index.test.ts (5 tests) 🦀 FROM RUST: Adding 2 + 3 🦀 FROM RUST: Adding 5 + 5 ======= PASS =======
This demonstrates how you can debug Rust code using web_sys::console::log_1
to output messages to the JavaScript console when working with WebAssembly.
- Node.js 18+
- Rust (latest stable)
- wasm-pack
- python-rust-mvp - A companion project showing Rust integration with Python using PyO3
This project is licensed under the MIT License - see the LICENSE file for details.