Skip to content

tony/nodejs-rust-mvp

Repository files navigation

Node.js-Rust-MVP

Minimal Rust-Node.js example using wasm-bindgen and TypeScript.

Setup

make setup
make build
make test

Usage

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

Run the Example

To run a simple example that demonstrates the functionality and performance:

make example

Development Demo

Here's a quick demonstration of the development workflow with Rust and WebAssembly integration:

  1. 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"] }
  2. 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
     }
  3. Rebuild and run tests with console output visible:

    make rebuild; RUST_BACKTRACE=1 yarn test
  4. 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.

Requirements

  • Node.js 18+
  • Rust (latest stable)
  • wasm-pack

See Also

  • python-rust-mvp - A companion project showing Rust integration with Python using PyO3

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Minimal Rust-Node.js example using wasm-bindgen and TypeScript.

Topics

Resources

License

Stars

Watchers

Forks