Skip to main content

run

Attribute Macro run 

Source
#[run]
Expand description

Attribute macro that wraps a function body in [run].

#[go_lib::run]
fn main() {
    let (tx, rx) = go_lib::chan::chan::<i32>(0);
    go_lib::go!(move || tx.send(42));
    println!("{}", rx.recv().unwrap());
}

See the [go_lib_macros::run][main] documentation for the full expansion rules and return-type support. Attribute macro that wraps a function body in [go_lib::run].

The macro rewrites

#[go_lib::run]
fn main() {
    /* body */
}

into

fn main() {
    go_lib::run(move || {
        /* body */
    })
}

When the function has a return type the closure is annotated with the same type so that ? and explicit return expressions work as expected:

#[go_lib::run]
fn main() -> Result<(), MyError> {
    do_work()?;
    Ok(())
}
// expands to:
fn main() -> Result<(), MyError> {
    go_lib::run(move || -> Result<(), MyError> {
        do_work()?;
        Ok(())
    })
}

Function parameters (if any) are captured by the move closure, so the macro also works on non-main entry points or helper functions.

§Errors

Emits a compile error if the function is async (go-lib provides its own concurrency model and does not interact with an async executor).