Eli5: Yew Crate

I have read the blurb for Yew multiple times and I still have no clue what it does! :thinking:

Thanks in advance.

Which blurb?

The documentation here https://docs.rs/yew/0.17.3/yew/ says:

Yew is a modern Rust framework for creating multi-threaded front-end web apps using WebAssembly

So basically it is Rust code that can be compiled and run in a web browser that generates all the elements of a web page that you may want. It does this by being compiled to "web assembler" (WASM) rather than any real machine code. like x86. WASM can be run in the browser like Javascript can.

Not only that it hooks up events like mouse clicks to functions that can change the content on the web page.

Typically one did this kind of thing in web browsers by writing HTML and Javascrip. With Yew you can do it in Rust. Notice the example code at the link above includes what looks very much like HTML tags wrapped in a html! Rust macro.

Ah, so it is like React, except that it compiles to WASM so that it can perform calculations and eventually asks the browser for DOM elements to display the results?

It's wasm_bindgen on steroids?

Also, if WASM is single threaded, how does Yew claim to be multi?

Yes, I kind of React in Rust.

It uses web workers apparently:

  • Achieves high performance by minimizing DOM API calls for each page render and by making it easy to offload processing to background web workers.

Just so that dumb me fully understands...

So I would learn the API of Yew, do the work I would normally do in HTML/CSS/JS, but with Rust and the Yew crate. Compile to wasm32-unknown-unknown, Then either create a shell index.html page to load up that wasm binary, or use something like Rocket/Actix to create a program that sits on a server waiting for requests, and that second React/Actix program I create will know how to serve up a webpage with the precompiled wasm file already embedded

...?

Indeed you would use Yew instead of writing HTML and JS. I'm not clearn on CSS there.

Building it into something a web browser can load and run is a bit complicated. I guess one could do it manually as you describe but they provide wasmpack to build it all, wrap it in whatever minimal Javascrip is required to get it started in the browser and interface between WASM and the DOM.

You can read all about how to do it in the getting started docs here: https://yew.rs/docs/en/getting-started/build-a-sample-app/. It's a work flow similar to using webpack with React or whatever.

You can serve the resulting bundle from whatever server you like.

Thanks man.

So Rocket says:

Rocket is a web framework for Rust that makes it simple to write fast , secure web applications

But that's not true, right, Rocket is to create a web server, not a web app? right? Yew tackles the web app portion, right?

A traditional web app consists of a backend and a frontend. Rocket lets you create the backend part, but if the backend is also the one doing the HTML rendering, then it also takes part in generating the frontend.

I would not say it is not true. Depends on what you mean by "app" (God I hate that) and what you expect such a system to encompass.

What we have is the World Wide Web. That is web servers, like Rocket and many others, serving content to web browsers like Firefox, Chrome etc using the HTTP protocol.

That content is of course HTML for page rendering, CSS for styling, along with images and audio and video, pretty much anything.

And of course, traditionally, Javascript to deliver code for the browser to run.

At this point you can make a simple "app" entirely as a program in JS, delivered by the web server that does all it's "app" work in the browser.

But of course, as such JS apps get more complex they rely on communication back to the server(s), think gmail, facebook, this forum etc, etc.

So of course people start to integrate both the server side and the browser side into some over reaching framework. Think Wordpress.

Personally I'm not into such big framework ideas. I want to be able to change my web server from Apache, to nginx, to node,js, to Rocket, or whatever. Or use a mix of those as suites what I am doing. And now a days I can think about using Rust and WASM instead of Javascript. Or both.

What is it you actually have in mind for your "app"?

1 Like

I don't have a particular app in mind. But I'm just sort of working backwards trying to understand everything in order to become a good programmer.

Perhaps you could say I'm working towards becoming a 'full stack' web developer.

I never had any formal Computer Science training, and although programming syntax and basic programming patterns are not that hard to pick up, actually knowing what you are doing seems to be a monster task.

1 Like

Welcome to the club.

I would wager that most of the world's working programmers have no formal qualification in Computer Science. Or even just programming for that matter.

Indeed "knowing what you are doing" is a monster task. Especially since everything in the programming world changes so fast all the time.

Rust is a case in point. Did not exist a short while ago. Still unknown to most. May be a huge thing in the coming years. May not be, given all the competition from from all the other new kids on the block, Go, Swift, Kotlin, God knows what.

Heck I read somewhere that Microsoft loves Rust for it's memory safety, but sure enough they were already thinking of developing their own language that does that. Kind of like how they made C# instead of using Java. Same but different, just because.

I'm not sure what to advise. I have been around the block enough times that I don't care anymore, I go with what interests me. Which happens to be Rust at this time as it is the first language I have used in four decades that actually offers genuinely new features with respect performance and program correctness.

Yes, it is hard.

1 Like

Well yes, but don't get tunnel vision. I don't need to tell you this, but little of Rust is actually fundamentally new. Most of the concepts are still the same, the protocols are still the same, what you can do with it is still fundamentally the same. A good programmer could pick up and become productive in Rust in a relatively short amount of time.

Which is why an investment in Rust for me, is an investment in myself as a programmer, and one that I'm happy to keep following.

I hope to go pro soon, but even if the professional world doesn't want me writing Rust, it won't be too hard to pick up the syntax of another language and just do it in that.

You do indeed need to tell me something.

The whole idea of Rust's anti-aliasing rules, aka the "borrow checker" is fundamentally new. That might seem like a small thing but it's huge.

The mere fact that Rust ensures safety and correctness in the use of memory whilst at the same time delivering the performance of a language like C is unique.

Maybe that capability has existed in some research languages buried away in some universities somewhere but it for sure is not in any commonly used programming languages.

I do agree that at the end of the day Rust does not have super powers to logically do things other languages cannot. After all a PC is a PC, a Turing Machine is a Turing Machine.

And yes, I would expect any programmer with whatever language background to be able to get into Rust.

1 Like