Skip to main content

scope

Function scope 

Source
pub fn scope<'env, F, R>(f: F) -> R
where F: for<'scope> FnOnce(&'scope Scope<'scope, 'env>) -> R,
Expand description

Spawn short-lived goroutines that can borrow data from the calling scope.

A thin re-export of scope::scope — see that module for full documentation, examples, and the lifetime-safety argument.

§Quick example

go_lib::run(|| {
    let data = vec![1_i64, 2, 3, 4, 5];

    let sum = go_lib::scope(|s| {
        let h1 = s.go(|| data[..3].iter().sum::<i64>());
        let h2 = s.go(|| data[3..].iter().sum::<i64>());
        h1.join().unwrap() + h2.join().unwrap()
    });

    assert_eq!(sum, 15);
});