NOTE: We're in the middle of merging changes and making fixes to support our upcoming 0.1.0 release. Some of the examples may be in a broken state. You can continue using the 0.0 releases with no issues.

Leptos Logo

crates.io docs.rs Discord

Leptos

```rust use leptos::*;

[component]

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: " {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() { mounttobody(|cx| view! { cx, }) }

```

About the Framework

Leptos is a full-stack, isomorphic Rust web framework leveraging fine-grained reactivity to build declarative user interfaces.

What does that mean?

Learn more

Here are some resources for learning more about Leptos:

nightly Note

Most 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:

  1. You need to enable the "stable" flag in Cargo.toml: leptos = { version = "0.1.0-alpha", features = ["stable"] }
  2. 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

FAQs

Can I use this for native GUI?

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.

How is this different from Yew/Dioxus?

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:

How is this different from Sycamore?

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: