I recently decided to switch the engine of Boardgame Lab from TypeScript to Rust. The application itself is an SPA written in Svelte. I only switched the logic that updates the game state to Rust. Here is a summary of my experience with the transition:

Isomorphic Architecture

One of the advantages of using JavaScript or TypeScript is that you can run the same code on both client and server. For Boardgame Lab, this means that the game state can be updated independently on both sides. This results in a lag-free experience on the client while still using the server as the authoritative source of data.

With WebAssembly taking off, you can get the same advantage writing Rust. Instead of pushing client-side code to the server, we push server-side code to the client by compiling Rust to WebAssembly.

This Rollup plugin allows you to import a Cargo.toml file into your TypeScript codebase, allowing a seamless integration between Rust and TypeScript code. The dev experience is almost as smooth as writing TypeScript itself (for example, the browser refreshes automatically when you change a line of Rust code).

wasm-bindgen facilitates serialization of Rust structs into JSON objects and vice-versa.

Limitations of TypeScript

Coding in TypeScript has been a largely pleasant experience, but switching to Rust immediately brought to light some of the limitations of TypeScript.

#typescript #rust #webassembly

Moving from TypeScript to Rust / WebAssembly
42.05 GEEK