Discuss a project

Blog / Technologies

Leptos: full-stack Rust without a virtual DOM

Leptos is one of the most visible web frameworks in Rust: the server renders the pages in Rust, and the interactive parts come alive in the browser through WebAssembly. One language, one type system and one ecosystem for the whole stack. Let us look at how the framework works, how it differs from the familiar JavaScript tools and in which projects it makes sense.

Leptos is a full-stack framework: it covers both server rendering and client interfaces. Its key idea is to bring fine-grained reactivity to Rust, the approach made famous by SolidJS, which the authors of Leptos name as their inspiration, and to add SSR, hydration and server functions out of the box.

Signals instead of a virtual DOM

In React a change of state re-renders a component: the virtual DOM is compared with its previous version, and the difference is applied to the page. Leptos takes another path. State lives in signals, and every piece of the DOM subscribes only to the signals it depends on. When a signal changes, exactly one text node or attribute is updated. There is no diffing and no second run of the component at all.

use leptos::prelude::*;

#[component]
pub fn Counter() -> impl IntoView {
    let (count, set_count) = signal(0);

    view! {
        <button on:click=move |_| set_count.update(|n| *n += 1)>
            "Count: " {count}
        </button>
    }
}

The expression {count} inside view! subscribes to the signal directly: a click updates only the text of the button, not the whole component. Thanks to the macro the markup is checked by the compiler: a typo in an attribute or a mismatch in the types of the props is a build error, not a bug at run time in the browser.

The model borrowed from Solid gives familiar ergonomics: signals, memos, effects. More on this approach in the JavaScript world is in our review of SolidJS.

One stack: SSR, hydration and server functions

Leptos renders the page on the server, sends ready HTML and brings it to life in the browser: hydration without a second render. For the client to talk to the server there are server functions:

#[server]
pub async fn subscribe(email: String) -> Result<bool, ServerFnError> {
    // runs only on the server: the database, SMTP, secrets
    save_subscriber(&email).await?;
    Ok(true)
}

From the outside it is an ordinary async function: the client calls it as if it were local, and the compiler generates an RPC wrapper. The body runs only on the server, where the database, sending mail and everything that must not reach the browser are available. Under the hood Leptos integrates with Axum or Actix Web.

The build of the two targets is described declaratively:

[features]
hydrate = ["leptos/hydrate", "dep:wasm-bindgen"]
ssr     = ["dep:axum", "dep:leptos_axum", "leptos/ssr"]

The cargo-leptos tool builds the server binary and the WASM bundle for the browser. For the client profile people turn on opt-level = "z" and LTO, and with these settings the WASM stays compact. And thanks to SSR and hydration the browser does not download extra JavaScript: the frame of the page arrives ready from the server, and neither the templates nor the data are moved to the client.

Strong and weak sides

  • Fine-grained reactivity: DOM updates without diffing, a fast response
  • One language and one type system for the client and the server: refactoring across the whole stack
  • SSR, SEO and server functions out of the box
  • A compact runtime: no virtual DOM shipped to the browser
  • Markup and type errors are caught at compile time

The other side:

  • The ecosystem is much smaller than that of React or Vue: fewer ready UI kits and integrations
  • A higher entry barrier: you need both Rust with its borrow checker and the reactive way of thinking
  • Hiring web developers who write Rust is harder than for the mainstream stacks
  • JS libraries (maps, chats, analytics) have to be wrapped by hand

When to choose Leptos

Leptos is good when a project needs both SEO and a live interface: landing pages with forms, SaaS dashboards, admin panels, internal services. It pays off most for teams that already write their backend in Rust: full stack in one language removes a whole layer of problems at the seam. If the task is a mass product built from ready components and the market dictates JavaScript, the maturity of the React ecosystem still outweighs it.

Conclusion

Leptos is the most complete answer to the question "why Rust on the web": one code base serves both the server and the browser, and performance and type safety stop being a compromise. The framework is still young and its ecosystem is catching up with the mainstream, but the future of the typed web belongs to tools like this one.

Shall we discuss your project?

Tell us about your task and we will come back with an estimate of the time and the cost within two working days.