Skip to main content
Deno 2 is finally here 🎉️
Learn more

🦕

Build Rust functions for Deno

Usage:

You must have wasm-bindgen-cli installed on your machine. To install run cargo install wasm-bindgen-cli.

Add wasm-bindgen to your project

In your cargo.toml file, you need to specify the crate type

[lib]
crate-type = ["cdylib"]

under dependencies add:

wasm-bindgen = "0.2"

Import wasm-bindgen to your rust file:

use wasm_bindgen::prelude::*;

Add the macro to your functions:

#[wasm_bindgen]
pub fn my_awesome_function() -> String {
    "This is from my awesome function in Rust!"
}

Install the script

deno install --allow-read --allow-write --allow-run -n deno-build-wasm https://deno.land/x/deno_build_wasm/mod.ts

Build your functions

deno-build-wasm my-awesome-functions

This will generate a pkg directory with the neccessary files.

Use your functions in Deno

import { my_awesome_function } from './pkg/my-awesome-functions.js'

console.log(my_awesome_function())
// Prints: This is from my awesome function in Rust!

 

Huge thanks to @SrKomodo for the original script!