this post was submitted on 17 Sep 2023
48 points (100.0% liked)
Programming
13376 readers
1 users here now
All things programming and coding related. Subcommunity of Technology.
This community's icon was made by Aaron Schneider, under the CC-BY-NC-SA 4.0 license.
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Why do you prefer WASM over JavaScript (which is suppose is your alternative for the UI work)? I build a lot of web stuff but haven't tried WASM yet.
Well, I'm mostly a backend dev, so my favorite part is that I don't have to write JavaScript. 🙃
You might be more adept at navigating it, but that language is just a minefield of unexpected behaviour and has a massive, complex ecosystem.
I'm using Rust to compile to WASM (has the most mature tooling for that, as far as I'm aware) with Leptos as the UI framework.
You write your HTML and CSS like normal, but JS is replaced with Rust code.
And well, one big advantage is that since my backend is in Rust, too, I can use the exact same models, constants etc. in backend and frontend. If they drift apart, that's a compile error.
(You do get this advantage, too, if you use NodeJS for the backend.)
But aside from me not liking JavaScript, there's lots of little things that make Rust a pleasure to use here.
Rust's sum types (Option, Result etc.) match really nicely with the infos you need for crafting a UI. Like, just yesterday I simply wanted to load a list of songs (
Vec
) and thought, I'll do the error handling later, but Rust's paradigms still eventually left me with aResult, Error>
(contains either the song list or an error message).At that point, I could have still just told it explode, if there's an error, but then I realized, oh yeah, if I can't load the list of songs, that
Error
is precisely what I want to show to the user.And then you don't have to deal with horrid ternaries. Instead you get full pattern matching, so my UI code now looks roughly like this:
I did leave out quite a bit of the boilerplate there, so this isn't valid code, but point is, it's a hundred times better than passing data and error in separate variables, separately checking, if they're non-null and never quite knowing what all the legal states are, since both could be intentionally null or uninitialized null or unintentionally null or both non-null. It's so much complexity that you just don't have.
And ultimately, yeah, I chose the word "fun" quite intentionally there. You get the cool parts of frontend, i.e. lots of visual feedback and something to show, while leaving out most of the downsides, i.e. fugly code.
There is, of course, different downsides to the WASM/Rust/Leptos stack, like it simply being a very new ecosystem. You can't yet pull in dozens of component libraries and so may have to build things more often yourself. And it isn't yet as tried-and-tested as some of the bigger frameworks, plus Leptos is still going through breaking changes.
So, yeah, maybe don't yet switch over everything at your dayjob, and of course, if you know Rust already, that's quite the advantage, but overall, I do quite recommend it and feel like it'll become a lot more relevant in the future.
Thanks for the elaborate reply! All my backend stuff is in Python (Django) but I think there is python WASM frontend stuff too so I think quite some of your insights might apply. Python is not quite as secure-by-design (secure as in I know for sure how my code behaves and where to expect exceptions) as rust but still better than JavaScript so I might look into that!