Simple Rust WebAssembly Tutorial Request

Hello,

I have been looking at examples on how to compile a simple rust lib with a helper function to webassembly. Unfortunately, many of the examples that I have found are either out of date or broken.

I have seen several examples that walk the user through the FFI creation, etc. However, when it comes to trying to actually use it inside of an html script tag, it never works properly.

Does anyone have an up to date resource on how to do this?

I would appreciate it. Thank you.

1 Like

The thing that worked for me is using the neat wargo with the rusty web. Unfortunately it is still very barebone, i.e. you can use C ffi function which need to manually declared both in rust and js. I'd prefer using stdweb, but unfortunately, wasm support seems to be broken.

So I agree with you that a comprehensive, up-to-date guide on the wasm is required.

1 Like

Thank you very much for your links! The Rustyweb link was actually helpful (specifically the library example), and I got a working example!

However, it definitely incomplete or outdated. For starters, it did not recommend the 'wasm32-unknown-emscripten' target, instead, it recommends asm. Also, I was unable to get it to work out of the box, I ended up needed to do the following inside my tags. since without the onRuntimeInitialized function, it did not work.

        Module.onRuntimeInitialized = () => {
            const testing = Module.cwrap("testing", "", []);
            testing();
        };

With the latest addition to the Rust toolchain, you can now compile directly to wasm. Here is a working example https://github.com/steveklabnik/semver.crates.io/commit/dc3bfad678bfdedcc90105ccfdf0d063737a7006

We've written up a more in depth tutorial that implements Conway's Game of Life in Rust and WebAssembly. See this thread for details:

4 Likes

Please see this mini-demo, I used as hands-on compiling to wasm-target.

My intention was to get the code as small as possible, but the panic-handler caused some overhead. AFAIK, this has been improved recently (I can't find the link)