Please note: This framework is in active development. I'm keeping it in a cycle of 0.0.x releases at the moment to indicate that it’s not even ready for its 0.1.0. Active work is being done on documentation and features, and APIs should not necessarily be considered stable. At the same time, it is more than a toy project or proof of concept, and I am actively using it for my own application development.
use leptos::*;
#[component]
pub fn SimpleCounter(cx: Scope, initial_value: i32) -> Element {
// create a reactive signal with the initial value
let (value, set_value) = create_signal(cx, initial_value);
// create event handlers for our buttons
// note that `value` and `set_value` are `Copy`, so it's super easy to move them into closures
let clear = move |_| set_value(0);
let decrement = move |_| set_value.update(|value| *value -= 1);
let increment = move |_| set_value.update(|value| *value += 1);
// this JSX is compiled to an HTML template string for performance
view! {
cx,
<div>
<button on:click=clear>"Clear"</button>
<button on:click=decrement>"-1"</button>
<span>"Value: " {move || value().to_string()} "!"</span>
<button on:click=increment>"+1"</button>
</div>
}
}
// Easy to use with Trunk (trunkrs.dev) or with a simple wasm-bindgen setup
pub fn main() {
mount_to_body(|cx| view! { cx, <SimpleCounter initial_value=3 /> })
}
Leptos is a full-stack, isomorphic Rust web framework leveraging fine-grained reactivity to build declarative user interfaces.
Resource
s) and HTML (out-of-order streaming of <Suspense/>
components.)Here are some resources for learning more about Leptos:
nightly
NoteMost of the examples assume you’re using nightly
Rust. If you’re on stable, note the following:
"stable"
flag in Cargo.toml
: leptos = { version = "0.0", features = ["stable"] }
nightly
enables the function call syntax for accessing and setting signals. If you’re using stable
,
you’ll just call .get()
, .set()
, or .update()
manually. Check out the
counters-stable
example
for examples of the correct API.I’ve created a benchmark comparing Leptos’s HTML rendering on the server to Tera, Yew, and Sycamore. You can find the benchmark here and run it yourself using cargo bench
. Leptos renders HTML roughly as fast as Tera, and scales well as templates become larger. It's significantly faster than the server-side HTML rendering done by similar frameworks.
ns/iter | Tera | Leptos | Yew | Sycamore |
3 Counters | 3,454 | 5,666 | 34,984 | 32,412 |
TodoMVC (no todos) | 2,396 | 5,561 | 38,725 | 68,749 |
TodoMVC (1000 todos) | 3,829,447 | 3,077,907 | 5,125,639 | 19,448,900 |
Average | 1.08 | 1.65 | 6.25 | 9.36 |
The gold standard for testing raw rendering performance for front-end web frameworks is the js-framework-benchmark. The official results list Leptos as the fastest Rust/Wasm framework, slightly slower than SolidJS and significantly faster than popular JS frameworks like Svelte, Preact, and React.
Sure! Obviously the view
macro is for generating DOM nodes but you can use the reactive system to drive native any GUI toolkit that uses the same kind of object-oriented, event-callback-based framework as the DOM pretty easily. The principles are the same:
I've put together a very simple GTK example so you can see what I mean.
On the surface level, these libraries may seem similar. Yew is, of course, the most mature Rust library for web UI development and has a huge ecosystem. Dioxus is similar in many ways, being heavily inspired by React. Here are some conceptual differences between Leptos and these frameworks:
Conceptually, these two frameworks are very similar: because both are built on fine-grained reactivity, most apps will end up looking very similar between the two, and Sycamore or Leptos apps will both look a lot like SolidJS apps, in the same way that Yew or Dioxus can look a lot like React.
There are some practical differences that make a significant difference:
view
macro. Sycamore offers the choice of its own templating DSL or a builder syntax.view
macro compiles to a static HTML string and a set of instructions of how to assign its reactive values. This means that at runtime, Leptos can clone a <template>
node rather than calling document.createElement()
to create DOM nodes. This is a significantly faster way of rendering components.let (count, set_count) = create_signal(cx, 0);
(If you prefer or if it's more convenient for your API, you can use create_rw_signal
to give a unified read/write signal.)
count()
instead of count.get()
) This creates a more consistent mental model: accessing a reactive value is always a matter of calling a function. For example:let (count, set_count) = create_signal(cx, 0); // a signal
let double_count = move || count() * 2; // a derived signal
let memoized_count = create_memo(cx, move |_| count() * 3); // a memo
// all are accessed by calling them
assert_eq!(count(), 0);
assert_eq!(double_count(), 0);
assert_eq!(memoized_count(), 0);
// this function can accept any of those signals
fn do_work_on_signal(my_signal: impl Fn() -> i32) { ... }
'static
: Both Leptos and Sycamore ease the pain of moving signals in closures (in particular, event listeners) by making them Copy
, to avoid the { let count = count.clone(); move |_| ... }
that's very familiar in Rust UI code. Sycamore does this by using bump allocation to tie the lifetimes of its signals to its scopes: since references are Copy
, &'a Signal<T>
can be moved into a closure. Leptos does this by using arena allocation and passing around indices: types like ReadSignal<T>
, WriteSignal<T>
, and Memo<T>
are actually wrapper for indices into an arena. This means that both scopes and signals are both Copy
and 'static
in Leptos, which means that they can be moved easily into closures without adding lifetime complexity.