Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Signals

Signals are values with change tracking built in. In practice, this means you create a signal once, bind it to views and modifiers, and then update it as application state changes.

Creating a signal

let count = Signal::new(0);

Signals are Copy, so you can pass them into bindings and callbacks without cloning or borrowing state.

Signals are commonly stored on a model:

pub struct AppData {
    count: Signal<i32>,
}

impl Model for AppData {}

Signals are lightweight handles and are Copy, so you can pass them into bindings and callbacks without cloning or borrowing state.

Where to read and write

Capturing signals in closures

When a closure captures a signal, prefer move:

Binding::new(cx, count, move |cx| {
    if count.get(cx) > 10 {
        Label::new(cx, "Large value");
    }
});

Because signals are Copy, moving a signal into the closure just copies the handle.

See also