Skip to main content

with

Macro with 

Source
with!() { /* proc-macro */ }
Expand description

The with! macro for cloning state handles into closures.

State is a handle (like a smart pointer), not the data itself. When you need to use state in a closure, you must clone the handle so the closure owns its own copy. This macro makes that pattern concise.

ยงExamples

Single state:

โ“˜
let count = state!(cx, || 0);
let increment = with!(count => move || count.update(|n| *n += 1));

Multiple states:

โ“˜
let count = state!(cx, || 0);
let name = state!(cx, || String::new());

let handler = with!(count, name => move || {
    count.update(|n| *n += 1);
    name.set("updated".to_string());
});

The above expands to:

โ“˜
let handler = {
    let count = count.clone();
    let name = name.clone();
    move || {
        count.update(|n| *n += 1);
        name.set("updated".to_string());
    }
};