agentleFS
botirkhaltaevbotirkhaltaev/botir-skills/skills/rust-wasm/SKILL.md

rust-wasm

Skill4 starsChanged 4 months agoApache-2.0View on GitHub

Compile Rust to WebAssembly for browser and Node.js environments. Use when building wasm modules with wasm-bindgen, creating JS interop, using web-sys/js-sys, optimizing wasm size, or deploying Rust+WASM to production.

  • Installs packages

3 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

1 rule. 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 3 edited versions we read keep these.

  • 0

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use WebAssembly.instantiateStreaming for fastest loading.

    line 174

“Common” is measured: kept by at least 6 in 10 of the 3 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Rust & WebAssembly

Based on the Rust and WebAssembly book, the wasm-bindgen guide, and wasm-pack documentation.

When to Use This Skill

  • Compiling Rust to .wasm for the browser
  • Interoperating with JavaScript (functions, objects, strings)
  • Using DOM APIs from Rust via web-sys
  • Publishing Rust-WASM packages to npm
  • Optimizing .wasm binary size
  • Debugging and profiling WASM modules

Quick Start

# Install tooling
rustup target add wasm32-unknown-unknown
cargo install wasm-pack

# Create project
cargo new --lib my-wasm-lib
cd my-wasm-lib

Cargo.toml

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

[dependencies]
wasm-bindgen = "0.2"
web-sys = { version = "0.3", features = ["console", "Document", "Element", "Window"] }
js-sys = "0.3"

[profile.release]
opt-level = "s"     # optimize for size
lto = true

src/lib.rs

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn greet(name: &str) -> String {
    format!("Hello, {name}!")
}

#[wasm_bindgen]
pub struct Universe {
    width: u32,
    height: u32,
    cells: Vec<u8>,
}

#[wasm_bindgen]
impl Universe {
    pub fn new(width: u32, height: u32) -> Self {
        Self { width, height, cells: vec![0; (width * height) as usize] }
    }

    pub fn tick(&mut self) { /* game of life step */ }

    pub fn cells_ptr(&self) -> *const u8 { self.cells.as_ptr() }
}

Build & Use

wasm-pack build --target web      # for <script type="module">
wasm-pack build --target bundler  # for webpack/vite
wasm-pack build --target nodejs   # for Node.js

wasm-bindgen Attributes

Exporting to JS

#[wasm_bindgen]
pub fn add(a: u32, b: u32) -> u32 { a + b }

#[wasm_bindgen]
pub struct Counter { count: u32 }

#[wasm_bindgen]
impl Counter {
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self { Self { count: 0 } }

    pub fn increment(&mut self) { self.count += 1; }

    #[wasm_bindgen(getter)]
    pub fn count(&self) -> u32 { self.count }
}

Importing from JS

#[wasm_bindgen]
extern "C" {
    fn alert(s: &str);

    #[wasm_bindgen(js_namespace = console)]
    fn log(s: &str);

    #[wasm_bindgen(js_namespace = Math)]
    fn random() -> f64;
}

DOM Manipulation (web-sys)

use web_sys::{window, Document, Element};

pub fn create_element() -> Result<(), JsValue> {
    let document = window().unwrap().document().unwrap();
    let div = document.create_element("div")?;
    div.set_text_content(Some("Hello from Rust!"));
    document.body().unwrap().append_child(&div)?;
    Ok(())
}

Size Optimization

# Cargo.toml
[profile.release]
opt-level = "z"       # aggressive size optimization
lto = true
codegen-units = 1
strip = true
# Further reduce with wasm-opt (from binaryen)
wasm-opt -Oz -o output.wasm input.wasm

# Analyze what's taking space
cargo install twiggy
twiggy top pkg/my_lib_bg.wasm
twiggy dominators pkg/my_lib_bg.wasm

Avoid:

  • format! / panic! messages (pull in formatting machinery)
  • std::collections::HashMap (large; use a simpler map)
  • String-heavy APIs (prefer passing indices/pointers for hot paths)

Deployment

wasm-pack build --target web --release
# Produces pkg/ directory with:
#   my_lib.js       — JS glue
#   my_lib_bg.wasm  — compiled module
#   my_lib.d.ts     — TypeScript definitions

Serve with correct MIME type: application/wasm

Use WebAssembly.instantiateStreaming for fastest loading.

Reference Map

  • references/wasm-bindgen-api.md — attributes, types, conversions
  • references/web-sys-dom.md — DOM, events, fetch, canvas
  • references/optimization-debugging.md — size profiling, debugging, testing

Key References

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.