State of Wasm for Rust

I was interested in checking out the ability to build web based front ends using the cargo-web crate, but the project appears to be in an abandoned state.

For example:

cargo web install

Fails to build on linux.

Does anyone have any news about any currently active projects using rust in this way?

I'd appreciate any advice or information on this.

It's worth noting that the Packt book "Creative Projects for Rust Programmers" which I purchased has an entire Chapter dedicated to using the this technology. Written way way back in the year 2020, so perhaps the world has simply moved on!

Thank you

I use trunk

I'm building a frontend using seed that talks to an axum backend (was using actix-web until recently).

I'm not a user of cargo-web, but it seems to me it isn't really required for developing a front-end. Since the title suggests you are interested in WebAssembly, I can tell you my own experience with wasm.

Rust WebAssembly support seems great in that the built-in cross-compiler targets Just Work™, and most crates in the ecosystem support it. For running in the browser, the wasm32-unknown-unknown target needs to be used (which is more barebones, without support for e.g. filesystem I/O), so you should definitely check that before using a crate.

One potential problem with wasm support is depending on C libraries. From what I can tell, this simply doesn't work currently (due to some fairly obscure problems with ABI compatibility), or rather, it works through the emscripten target, which appears to be deprecated.

Other than that, wasm-pack is all you need to have your WebAssembly packaged up into a convenient, directly importable pair of {.wasm,.js} files and off you go.

Ps.: one thing I found out quickly is that passing complex structures between wasm and Rust doesn't always work with wasm-bindgen (you'll get compile errors if you want anything more complicated than a Vec<f64> or similar). Again, this is an ABI problem that is being worked on, but in the meantime, if you run into this kind of situation, don't panic – serialize to JSON!

1 Like

I find the best way to get up and running is using the roll-up plugin: GitHub - wasm-tool/rollup-plugin-rust: Rollup plugin for bundling and importing Rust crates.

Assuming you're talking about tagetting regular DOM stuff (gamedev/WebGL/webgpu would be a different approach...)

A large example of essentially full-stack Rust is under development here.. I plan to do a larger write-up about the tech stack when the project is ready for release: GitHub - ji-devs/ji-cloud (it's a bit disingenuous to say "full stack" since we bail out to lit-element for encapsulating CSS.. but that's a larger discussion that would take this on a tangent)

I like the Dominator framework for frontend: GitHub - Pauan/rust-dominator: Zero-cost ultra-high-performance declarative DOM library using FRP signals for Rust!

There aren't a ton of docs, I keep some rough notes in this gist: Dominator notes.md · GitHub

If you want to work without a framework, aside for native web_sys, you're going to want to do some things via gloo: GitHub - rustwasm/gloo: A modular toolkit for building fast, reliable Web applications and libraries with Rust and WASM

I have some additional Rusty wrappers around web stuff here: GitHub - dakom/awsm-web

2 Likes

Btw this was my first Rust project, and I'd say it's the absolute worst way to learn Rust, hehe... doing it in the browser forces a lot of tough things early on that could be avoided by starting with cli tools or whatever (async everywhere, trying to pass data across FFI boundary, mis-match between application state and UI state, etc.)

(also just noticed the demo build is failing in CI, and the view source links are outdated... I run the demo locally whenever I make tweaks to the crate, and there's more than what's visible in the live link)

I vaguely remember that last time I tried WASM and Rust on FreeBSD, I had a compilation issue (but not sure if I remember right).

I just tried to follow the tutorial here to confirm: Rust and WebAssembly Edit: correct link to beginning of tutorial.

Clone the project template with this command:

cargo generate --git https://github.com/rustwasm/wasm-pack-template

I get:

% cargo generate --git https://github.com/rustwasm/wasm-pack-template
error: no such subcommand: `generate`
% cargo --version
cargo 1.60.0-nightly (1c034752d 2022-01-25)

cargo generate is not a built-in command. They even write at the top of that page that you have to follow the setup instructions, which include installing cargo-generate.

1 Like

Oh, I should have followed the steps more precisely then! My apologies!

1 Like

Now I hang at:

% rustup toolchain install wasm32-unknown-unknown
error: invalid toolchain name: 'wasm32-unknown-unknown'

Maybe I need a different syntax?

That's not a toolchain, that's a target: rustup target -h

1 Like

I get:

 % wasm-pack build
[INFO]: Checking for the Wasm target...
[INFO]: Compiling to Wasm...
warning: function is never used: `set_panic_hook`
 --> src/utils.rs:1:8
  |
1 | pub fn set_panic_hook() {
  |        ^^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: `wasm-game-of-life` (lib) generated 1 warning
    Finished release [optimized] target(s) in 0.01s
[INFO]: Installing wasm-bindgen...
Error: no prebuilt wasm-opt binaries are available for this platform: Unrecognized target!
To disable `wasm-opt`, add `wasm-opt = false` to your package metadata in your `Cargo.toml`.

Anyway, maybe I wasn't doing everything right. I might retry when I have some more time to thoroughly test it.

P.S.: Does someone happen to know if Rust + WASM work on FreeBSD?

You should also install the WebAssembly Binary Toolkit and Binaryen for a complete WASM toolchain.

2 Likes

Interesting. Thank you. I'm using actix-web currently. Could I ask why you switched to axum?

Thank you very the detailed reply. I am fairly new to rust, and love the language. I've been around the block a couple of times with Angular, and frankly not happy with the overall nature of that experience. It's cool stuff, but the idea of 100% rust code, is extremely appealing to me.

As I stated in my initial post, I'm largely going on what appears to be the quite outdated chapter in the "Creative Projects for Rust Programmers". It was quite reliable for the chahpter on serde and the Nom parser combinator crate, but the Web assembly is clearly out of date, or leaves much to be desired in either case.

For example, it simply says to run "cargo install cargo-web" in order to load the rust ability to generate Web assembly code. Are you saying that this step is not even required?

The chapter goes on to have you use the Yew framework to build the application example.
I see that Yew has more recent updates, so perhaps my best step would be to follow the advise they have there for getting started in Yew.

1 Like

Okay. So I'm confused by what I read in that "Creative Projects for Rust Programmers", PackT book.

They say that you need to do cargo install cargo-web in order to pull down what's necessary for rust to generate Web assembly code.

Your comment suggests there are multiple choices, and you have found the best in your experience. Is that the one with a current and healthy community committed (for now at least) to pushing it forward?

I'd go with the rollup plugin mentioned above. It handles installation of wasm-pack and is really all you need.

I haven't used Yew so I can't make an informed comparison between it and Dominator, but I like Dominator a lot for Dom stuff.

Thanks for the tip. I'll take a look at Dominator.

I found this to be very helpful for using Rust to build WASM and publishing to the web: Run Your Rust Games in a Browser: Hands-on Rust Bonus Content · Hands-On Rust

Thanks!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.