Write and Compile Rust Code to WebAssembly:
Use Rust and tools like wasm-pack to compile your Rust code to WebAssembly.
Install wasm-pack if you haven't already:
cargo install wasm-packCreate a Rust Library Project with name rust_lib
cargo new --lib rust_lib
cd rust_libAdd the following to your Cargo.toml file:
[package]
name = "rust_lib"
version = "0.1.0"
edition = "2021"
[dependencies]
wasm-bindgen = "0.2"
[lib]
crate-type = ["cdylib"]Write Rust Code:
Create a Rust file (e.g., src/lib.rs) with your Rust code.
For example:
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn add(a: i32, b: i32) -> i32 {
let result:i32 = a + b;
return result;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add_positive_numbers() {
assert_eq!(add(3, 5), 8);
}
#[test]
fn test_add_negative_numbers() {
assert_eq!(add(-3, -5), -8);
}
#[test]
fn test_add_mixed_numbers() {
assert_eq!(add(-3, 5), 2);
}
#[test]
fn test_add_zero() {
assert_eq!(add(0, 0), 0);
}
#[test]
fn test_add_large_numbers() {
assert_eq!(add(1_000_000, 2_000_000), 3_000_000);
}
}Compile to WebAssembly with nodejs attribute:
If you were writing a package that should be used in Node.js (with CommonJS modules, e.g. require), you would run this in your terminal:
wasm-pack build --target nodejsfor ESM modules, run this in your terminal:
wasm-pack build --target webThis will generate a pkg directory containing the compiled WebAssembly code and JavaScript bindings.
Create a Typescript Project.
mkdir typescript_exec
cd typescript_exec
npm init -y
npm install --save-dev typescript
npx tsc --initNote: use current package.json and tsconfig.json file which provide in the repository.
Copy the pkg directory from the Rust project to the Typescript project.
Create a rust folder in the Typescript project and copy the pkg directory from the Rust project into it.
Note:rust folder will create in the root of the Typescript project.
Create a core/index.ts file in the src folder of the Typescript project.
Import the WebAssembly module in your TypeScript code:
import {add} from "../../rust/pkg/rust_lib";
export const main = async () => {
const value1:number = 1;
const value2:number = 2;
const result:number=add(value1, value2);
console.log(result);
};
main();Run the following command to run the Typescript code:
npm run debugNote: go to package.json and add the following script:
"scripts": {
"debug": "ts-node-dev --inspect=4321 --pretty --transpile-only ./src/core/index.ts",
}