October 13, 2022

Building web apps with WebAssembly, Rust and Yew

What is Yew?

Yew is a modern Rust framework for building multi-threaded front-end web apps with WebAssembly.

One of its features is declaring interactive HTML with Rust expressions. Developers who already have experience with JSX in React shouldn't have much trouble understanding Yew's syntax.

Yew performs well by minimizing DOM API calls on each page render, and it makes it easy to offload processing to web workers in the background.

It supports JavaScript interoperability, letting developers take advantage of NPM packages and integrate with existing JavaScript apps.

Prerequisites

To get started, let's make sure we have an up-to-date development environment. We'll need the following tools.

wasm32-unknown-unknown is the WASM compiler target for the Rust compiler.

This tutorial assumes you're already somewhat familiar with Rust. If not, read The Rust Programming Language.

Make sure you have the latest version of Rust by running rustup update, or install Rust. If everything is fine, we can start.

After installing Rust, you can use Cargo to install trunk with the command below:

cargo install trunk

We'll also add the WASM target by running the command below:

rustup target add wasm32-unknown-unknown

Setting up the project

First, we'll create the Cargo project.

cargo new yew-app
cd yew-app

To check that your Rust environment is set up correctly, run the starter project with cargo run. It should compile and print "Hello, world!"

Creating your first static page

To turn this simple command-line app into a basic Yew web app, a few changes are needed in your Cargo.toml file.

[package]
name = "yew-app"
version = "0.1.0"
edition = "2021"

[dependencies]
+ yew = "0.19"

With the Yew dependency and version in place, let's go to the main.rs file.

use yew::prelude::*;

#[function_component(App)]
fn app() -> Html {
    html! {
        <h1>{ "Hello World" }</h1>
    }
}

fn main() {
    yew::start_app::<App>();
}

We need to create the index.html file at the root of the project.

<!DOCTYPE html>
<html lang="en">
    <head> </head>
    <body></body>
</html>

Starting the development server

Run the following command to build and run the app locally.

trunk serve --open

Trunk will open your app in your default browser. It will keep watching your project and rebuild automatically when files change. To learn more about trunk, run trunk help and trunk help <subcommand>.

Yay

We've successfully set up our Yew development environment and built our first static page. Below I'll go over components a bit.

Writing HTML

Yew uses Rust's procedural macros and gives us a JSX-like syntax (a JavaScript extension that lets you write HTML-like code inside JavaScript) to build markup.

Everything inside the html! {} macro is converted into plain HTML. Below is a list of syntax rules that become quite simple after a few lines of code.

fn header() -> Html {
    html! { <header> {"Our Header"} </header> }
}

Components

Components are the building blocks of Yew apps. By combining components, which can themselves be made of other components, we build our application. By structuring our components for reuse and keeping them generic, we can use them in several parts of the app without duplicating code or logic.

In fact, the app function we've used so far is a component, called App. It's a "function component". There are two different kinds of components in Yew.

We can create the Title component and call it in the main render.

#[function_component(App)]
fn app() -> Html {
    html! {
        <>
+           <Title />
            <h1>{ "Hello World" }</h1>
        </>

    }
}

+ #[function_component(Title)]
+ pub fn title() -> Html {
+    html! {
+        <h1>{"Yew Exemple"}</h1>
+    }
+ }

First steps with WASM, Rust and Yew

Thanks for making it this far. With this simple app we get an idea of what we can build with these tools. If you want to dig deeper, I suggest reading the Yew documentation by clicking here.

Styling

Yew doesn't yet offer a built-in way to style components. There are community resources that can solve this; below is a list of tools.

Component libraries

CSS-in-Rust

My links

If you'd like to talk more about Rust and Yew, you can connect with me through the links below.

Twitter

Github

The code for this example is available on GitHub in the repository below.

https://github.com/santosant/yew-exemple

Source: https://dev.to/santosant/ptbr-criando-aplicacoes-web-com-webassembly-rust-e-yew-434e