百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

smithy

> 编程语言
开源

在 Rust 中构建 WebAssembly 应用程序的框架

343 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

在 Rust 中构建 WebAssembly 应用程序的框架

Smithy

Smithy is a front-end framework for Rust

Home page ◇ API Docs ◇ Repository ◇ Example Sites

What is Smithy?

Smithy is a framework for writing WebAssembly applications entirely in Rust. Its goal is to allow you to do so using idiomatic Rust, without giving up any of the compiler's safety guarantees.

Smithy works on nightly

Smithy v0.0.7 currently works on 1.39.0-nightly (dfd43f0fd 2019-09-01).

Getting started

Getting started in Smithy is easy!

bash
npm init smithy-app my_smithy_app
cd my_smithy_app
npm start

Navigate to localhost:8080 to see your app in action!

See the create-smithy-app repository for more details.

A simple Smithy app

A simple click counter is as follows:

rust
#[wasm_bindgen(start)]
pub fn start() -> Result {
  let root_element = get_root_element()?;
  let mut count = 0;
  let app = smithy::smd!(
    

  );
  smithy::mount(Box::new(app), root_element);
  Ok(())
}

fn get_root_element() -> Result {
  let document = web_sys::window().unwrap().document().unwrap();
  document.get_element_by_id("app")
    .ok_or(wasm_bindgen::JsValue::NULL)
}

How Smithy works

smd! macro

The smd! and smd_borrowed! macros convert something that looks like JSX into a wrapper around an FnMut(smithy::types::Phase) -> smithy::types::PhaseResult. For example, the smd! call in:

rust
let mut count = 0;
let app = smithy::smd!(
  

);

is converted into

…

Notice that the |_| count = count + 1 and {count} are in separate branches of the match arm. If they had not been (e.g. if smd! created a struct instead of an FnMut), this would not have compiled. The borrow checker would have complained that you cannot immutably borrow count, as it is already borrowed mutably in the on_click callback.

Smithy phases

As you can see from the expansion of the smd! macro above, phases are a core concept in Smithy. In particular, an app is driven through five phases:

  • rendering, in which the app is asked to return a struct containing the information about what it will write to the DOM.
  • ref assignment, in which any app with ref={&mut optional_web_sys_html_element} will have Some(some_html_element) assigned to that ref.
  • post rendering, in which any post_render={|_| ...} callbacks will be executed. These callbacks are guaranteed to have all refs already assigned, thus allowing you to do any direct DOM manipulation you need to do.
  • UI event handling and window event handling, in which Smithy executes callbacks in response to events. After a callback is executed, Smithy will re-run the app through the different phases.
    • (UI event handling and window event handling are treated as separate phases, though conceptually they are very similar.)

smd! vs smd_borrowed!

As you can see in the macro expansion above, the smd! macro creates a move closure. This is not always desirable. If you do not wish to create a move closure, use smd_borrowed! instead.

How to get involved

Smithy is always looking for contributors! Please tweet at me @statisticsftw or take a look at the Smithy roadmap.

In addition, please take Smithy out for a spin using create-smithy-app.

Thanks! Happy coding!

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rust

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言