wasm-snake
A small, playable Snake game with the rules written in Rust and compiled to WebAssembly. The browser shell is plain JavaScript: it draws the Rust state on a canvas and forwards keyboard and touch input to the game engine.
Requirements
Install mise. The project pins Rust 1.98.1, Node.js 26.7.0, wasm-pack 0.15.0, the wasm32-unknown-unknown Rust target, and exact JavaScript package versions in mise.toml, package.json, and package-lock.json.
Install and run
From this directory:
mise run install
mise run run
Open the local URL printed by Vite (normally http://127.0.0.1:5173). mise run run builds the Rust crate for WebAssembly in development mode and starts the browser dev server; it stays running until you stop it with Ctrl-C. mise run build produces an optimized WebAssembly module and static browser bundle in web/dist/.
Controls
- Arrow keys or WASD: steer. The on-screen directional pad also works on touch devices.
- Space: start or pause/resume.
- R or the Restart button: start a fresh run.
Collect the food, grow the snake, and avoid the walls and your own tail.
Checks
mise run check
This runs the native Rust game-logic tests, Clippy across all targets with all and pedantic enabled, builds the WebAssembly package, and verifies the production Vite bundle. Rust dependencies are pinned exactly in Cargo.toml and Cargo.lock; Cargo invocations use --locked.
Rust ↔ browser boundary
src/lib.rs owns movement, collision, food placement, score, lifecycle state, and restart. The wasm-bindgen Game API exposes a small indexed read interface for the snake and food, plus methods for starting, steering, ticking, and restarting. web/src/main.js owns the timer, keyboard/touch events, HUD, and canvas rendering. Game rules are unit-tested natively so they do not depend on a browser.