std-type

Scope: start scoped workers

A Scope is a borrowed spawning context, not a handle that can be stored beyond its lifetime. Separate local tasks can borrow the same immutable data. Expected stdout (not executed): (1, 9) Common mistake: Returning a scoped worker handle beyond the scope.

Formal Syntax & Structure

rust syntaxrust
Scope<'scope, 'env>; s.spawn(F)

Working Example

Example Snippetrust
fn main() {    let nums = [1, 4, 9];    let result = std::thread::scope(|s| {        let min = s.spawn(|| nums.iter().min().copied().unwrap());        let max = s.spawn(|| nums.iter().max().copied().unwrap());        (min.join().unwrap(), max.join().unwrap())    });    println!("{:?}", result);}
Rust Reference