```rust use leptos::*;
pub fn SimpleCounter(cx: Scope, initialvalue: i32) -> impl IntoView { // create a reactive signal with the initial value let (value, setvalue) = createsignal(cx, initialvalue);
// 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);
// create user interfaces with the declarative `view!` macro
view! {
cx,
<div>
<button on:click=clear>"Clear"</button>
<button on:click=decrement>"-1"</button>
<span>"Value: " {value} "!"</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() {
mounttobody(|cx| view! { cx,
```
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.
To set up your Rust toolchain using nightly
(and add the ability to compile Rust to WebAssembly, if you haven’t already)
rustup toolchain install nightly
rustup default nightly
rustup target add wasm32-unknown-unknown
If you’re on stable
, note the following:
"stable"
flag in Cargo.toml
: leptos = { version = "0.1.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.cargo-leptos
cargo-leptos
is a build tool that's designed to make it easy to build apps that run on both the client and the server, with seamless integration. The best way to get started with a real Leptos project right now is to use cargo-leptos
and our starter template.
bash
cargo install cargo-leptos
cargo leptos new --git https://github.com/leptos-rs/start
cd [your project name]
cargo leptos watch
Open browser on http://localhost:3000/
Leptos (λεπτός) is an ancient Greek word meaning “thin, light, refine, fine-grained.” To me, a classicist and not a dog owner, it evokes the lightweight reactive system that powers the framework. I've since learned the same word is at the root of the medical term “leptospirosis,” a blood infection that affects humans and animals... My bad. No dogs were harmed in the creation of this framework.
People usually mean one of three things by this question.
With 0.1 the APIs are basically settled. We’re adding new features, but we’re very happy with where the type system and patterns have landed. I would not expect major breaking changes to your code to adapt to, for example, a 0.2.0 release.
Yes, I’m sure there are. You can see from the state of our issue tracker over time that there aren’t that many bugs and they’re usually resolved pretty quickly. But for sure, there may be moments where you encounter something that requires a fix at the framework level, which may not be immediately resolved.
This may be the big one: “production ready” implies a certain orientation to a library: that you can basically use it, without any special knowledge of its internals or ability to contribute. Everyone has this at some level in their stack: for example I (@gbj) don’t have the capacity or knowledge to contribute to something like wasm-bindgen
at this point: I simply rely on it to work.
There are several people in this community using Leptos right now for internal apps at work, who have also become significant contributors. I think this is the right level of production use for now. There may be missing features that you need, and you may end up building them! But for internal apps, if you’re willing to build and contribute missing pieces along the way, the framework is definitely usable right now.
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.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.)Signals are functions: In Leptos, you can call a signal to access it rather than calling a specific method (so, 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:
rust
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) { ... }
Signals and scopes are '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 wrappers 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.