Yew — Rust WASM Framework for Web Applications
Yew is a Rust framework for creating reliable and efficient web applications that compile to WebAssembly. Component-based with a virtual DOM, hooks, and familiar React-like API. Write your entire frontend in Rust with type safety and performance.
Agent 可直接安装
这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。
npx -y tokrepo@latest install ce6e1482-3651-11f1-9bc6-00163e2b0d79 --target codex先 dry-run 确认安装计划,再运行此命令。
What it is
Yew is a Rust framework for creating reliable and efficient web applications that compile to WebAssembly. It provides a component-based architecture with a virtual DOM, hooks (use_state, use_effect, use_context), properties for data passing, and an event system. Yew's API is inspired by React, making it accessible to developers familiar with modern frontend patterns.
Yew targets Rust developers who want to build frontend web applications with the same language they use for backends, CLI tools, and systems programming. It suits single-page applications, interactive dashboards, and web-based tools where Rust's performance and type safety are advantages.
How it saves time or tokens
Yew's component model and hooks eliminate the need for JavaScript for interactive web UIs. Rust's type system catches errors at compile time that JavaScript would only reveal at runtime. The virtual DOM efficiently updates only changed parts of the UI. Trunk (the recommended build tool) handles WASM compilation, asset bundling, and development server with hot reload.
For AI-assisted development, Yew's React-like patterns (components, props, hooks) are familiar to LLMs, making code generation productive.
How to use
- Install Trunk:
cargo install trunkand add the WASM target:rustup target add wasm32-unknown-unknown. - Create a project with Cargo, add
yewas a dependency. - Build and serve:
trunk servefor development.
Example
use yew::prelude::*;
#[function_component(App)]
fn app() -> Html {
let counter = use_state(|| 0);
let onclick = {
let counter = counter.clone();
Callback::from(move |_| counter.set(*counter + 1))
};
html! {
<div>
<h1>{ "Counter" }</h1>
<p>{ format!("Count: {}", *counter) }</p>
<button {onclick}>{ "Increment" }</button>
</div>
}
}
fn main() {
yew::Renderer::<App>::new().render();
}
Related on TokRepo
- AI coding tools -- Rust and web development frameworks
- Featured workflows -- discover top curated assets on TokRepo
Common pitfalls
- Expecting npm package compatibility. Yew runs in WASM, not Node.js. JavaScript interop requires wasm-bindgen and web-sys crates, not npm packages.
- Large WASM binary sizes. Use
wasm-optand enable LTO in release builds to reduce binary size. Initial download is larger than equivalent JavaScript. - Thinking Yew replaces all JavaScript. DOM APIs not exposed by web-sys may require JavaScript glue code. CSS frameworks and existing JS libraries need wasm-bindgen interop.
常见问题
Yew is web-only (WASM) and has been around longer with a more mature ecosystem. Dioxus is multi-platform (web, desktop, mobile, TUI) and is newer. Yew uses an html! macro; Dioxus uses rsx!. Both provide React-like component models. For web-only projects, both work well; Yew has more community resources. For multi-platform, Dioxus is the better choice.
Yew is one of the most mature Rust WASM frameworks with a stable API and active community. Several projects use Yew in production. The main concerns are WASM binary size (larger initial download than JS) and the smaller ecosystem compared to React or Vue. For internal tools and applications where Rust's safety matters, Yew is a viable choice.
Yew provides the yew-router crate for client-side routing. Define routes as an enum, implement the Routable trait, and use the Switch component to render different components based on the URL. The router supports nested routes, query parameters, and programmatic navigation.
WebAssembly executes at near-native speed, making compute-heavy operations (data processing, canvas rendering, encryption) faster than JavaScript. For typical DOM manipulation, the performance difference is smaller because DOM operations are the bottleneck, not computation. Yew's virtual DOM minimizes DOM updates, which is where most UI frameworks spend time.
Yes. Use wasm-bindgen and js-sys/web-sys crates to call JavaScript functions from Rust. For existing JavaScript libraries (charting, maps, animations), create Rust bindings with wasm-bindgen that call the JS API. This enables gradual adoption where Yew handles the app structure while JS libraries provide specialized UI components.
引用来源 (3)
- Yew Official Website— Yew is a Rust framework for web apps compiled to WebAssembly
- Yew Documentation— Yew documentation and tutorial
- Yew GitHub— Yew GitHub repository
讨论
相关资产
Dioxus — Full-Stack App Framework for Web, Desktop, and Mobile
Dioxus is a full-stack app framework for Rust with a React-like API. Build web (WASM), desktop (native WebView), mobile (iOS/Android), TUI, and server-rendered apps from one codebase. Hooks, components, server functions, and hot reloading.
Robyn — High-Performance Python Web Framework with Rust Runtime
A fast Python web framework that uses a Rust-based async runtime for request handling while keeping the developer experience in pure Python.
wasm-bindgen — High-Level Rust and WebAssembly Interop
wasm-bindgen facilitates communication between Rust-compiled WebAssembly modules and JavaScript. It generates binding code that allows Rust functions to be called from JS and vice versa, handling type conversions, memory management, and DOM access automatically.
Farm — Extremely Fast Vite-Compatible Web Build Tool in Rust
A Rust-based web build tool that is fully compatible with the Vite plugin ecosystem while delivering significantly faster build and HMR performance.