In a Rust log-compaction tool, how does `std::thread::scope` let four worker threads borrow chunks of a local `Vec<Record>`, and what does it guarantee?
answer
- joined before the call returns
- bounds use 'scope, not 'static
- shared borrows to many, &mut to one
- stable since 1.63
basics
~20 sstd::thread::scope, stable since Rust 1.63, joins every thread spawned on its Scope before it returns, so those threads may borrow locals such as slices of a Vec. The borrow checker still applies: many shared borrows, or one mutable borrow, per value.
solid answer
~50 s`thread::scope(|s| { ... })` hands the closure a `&Scope`, and `s.spawn` starts threads whose closures only need `Send + 'scope` instead of `Send + 'static`. That is sound because `scope` waits for every thread it started before returning, so nothing they borrow can be freed while they run. For the compaction tool you split the `Vec<Record>` with `records.chunks(n)`, spawn one thread per chunk that counts the live records, and sum the results from each `ScopedJoinHandle::join`, or just let the scope join them. The usual borrow rules hold across threads: many threads may share `&records`, one thread may hold `&mut` to a disjoint piece (via `chunks_mut` or `iter_mut`), and the owner cannot mutate the `Vec` while a scoped thread still borrows it (E0502). After the scope returns, the borrows end and `records` is yours again. `thread::scope` was stabilised in Rust 1.63.
code
rust · 24 linesuse std::thread;
struct Record {
key: u32,
deleted: bool,
}
fn main() {
let records: Vec<Record> = (0..1_000)
.map(|i| Record { key: i, deleted: i % 3 == 0 })
.collect();
let chunk_len = records.len().div_ceil(4);
let live: usize = thread::scope(|s| {
let handles: Vec<_> = records
.chunks(chunk_len)
.map(|chunk| s.spawn(move || chunk.iter().filter(|r| !r.deleted).count()))
.collect();
handles.into_iter().map(|h| h.join().unwrap()).sum()
});
// The borrow ended with the scope, so `records` is usable again.
println!("live = {live} of {}, last key {}", records.len(), records[999].key);
}go deeper
Recall that thread::scope lets threads borrow locals because it waits for all of them before returning, unlike thread::spawn, which needs owned data.
Explain the Send + 'scope bound, the automatic join at scope end, splitting a Vec with chunks or chunks_mut, and why pushing to the Vec inside the scope fails with E0502.
Show when scope is the right join point and when it is not: it blocks the caller, creates fresh OS threads every call, and cannot host work that must outlive the function.
Weigh scoped threads against pools and owned-data designs: per-call thread creation cost, how many cores a request may claim, and keeping fork-join regions short and bounded.
## The problem scope solves `std::thread::spawn` requires its closure to be `'static`, because a spawned thread may outlive the function that started it. To share a `Vec<Record>` with workers you would have to move it in, clone it per thread, or wrap it in an `Arc`. Why `spawn` demands `'static` is covered with lifetimes; this question is about the alternative. **`std::thread::scope`**, stabilised in **Rust 1.63** (August 2022), lets threads borrow data that lives on the calling function's stack. Before that, the same idea came from crates such as crossbeam. ## How the API is shaped ```rust pub fn scope<'env, F, T>(f: F) -> T where F: for<'scope> FnOnce(&'scope Scope<'scope, 'env>) -> T; impl<'scope, 'env> Scope<'scope, 'env> { pub fn spawn<F, T>(&'scope self, f: F) -> ScopedJoinHandle<'scope, T> where F: FnOnce() -> T + Send + 'scope, T: Send + 'scope; } ``` - **`'env`** is the lifetime of whatever the threads borrow; it must outlast the whole call to `scope`, so locals declared just before the call qualify. - **`'scope`** is the lifetime of the scope itself: the window in which threads may be spawned and may still be running. It ends after `f` returns **and** every scoped thread has been joined, but before `scope` returns. - The thread closure only needs **`Send + 'scope`**, not `'static`. That single change in the bound is what allows `&records` or `&records[0..250]` inside a worker. - `scope` returns whatever the outer closure returns, so a total can flow straight out. ## The guarantee: every thread is joined before `scope` returns The standard library promises that all threads spawned in the scope that were not joined by hand are **automatically joined** before `scope` returns. The implementation catches a panic from the outer closure, waits until its count of running threads reaches zero, and only then returns or re-raises. Because the borrowed data outlives the call, and the threads cannot outlive the call, the borrows are always valid. One subtlety from the docs: joining waits for the thread's main function, and destructors of that thread's thread-local values may still be running after `scope` returns. ## Splitting a `Vec<Record>` across four workers The compaction tool wants the number of live (not deleted) records, computed by four threads: 1. Compute a chunk length with `records.len().div_ceil(4)`. 2. Inside `thread::scope`, map each `records.chunks(chunk_len)` slice to `s.spawn(move || ...)`, which copies the `&[Record]` slice reference into the thread. 3. Collect the `ScopedJoinHandle<usize>` values, `join` each and sum. 4. After the scope, use `records` freely again: the borrows ended with it. The borrow checker treats scoped threads like any other borrowers: | Access pattern | Allowed? | |---|---| | several threads read `&records` | yes, shared borrows | | each thread writes its own slot via `iter_mut()` or `chunks_mut()` | yes, disjoint `&mut` | | two threads take `&mut records` | no, rejected at compile time | | owner calls `records.push(..)` while a thread reads it | no, E0502 | | owner mutates `records` after `scope` returns | yes | The fourth row is the one that surprises people: inside the scope closure, the spawning thread is still a separate borrower, so `v.push(2)` after `s.spawn(|| v.len())` fails with E0502 even though everything is joined at the end. ## When to reach for scope, and when not - **Use it** for fork-join work over data you already own: split, compute in parallel, combine, continue. - **Use it** when the threads need `&mut` to disjoint parts of a buffer, which `spawn` cannot express without `Arc<Mutex<_>>`. - **Do not use it** for threads that must outlive the function, such as a background flusher; those need `thread::spawn` and owned data. - Remember that `scope` **blocks** the calling thread until all workers finish; it is a join point, not fire-and-forget. - Thread pools and parallel iterators, which reuse threads instead of creating four new ones per call, are a separate subject (Rayon). ## Results, handles and spawning from inside Each `s.spawn` returns a **`ScopedJoinHandle<'scope, T>`**. Its `join` returns `std::thread::Result<T>` exactly like a plain handle, so the compaction tool can sum four `usize` counts. A scoped thread may also capture `s` and spawn further threads into the same scope; they are joined at the same point. And because `scope` returns the outer closure's value, the common shape is a single expression: `let live = thread::scope(|s| ...);`.
- Can scoped threads write into a local array instead of returning values through `join`?Yes, as long as each thread gets a disjoint `&mut`. Zipping `totals.iter_mut()` with `data.chunks(25)` and spawning one thread per pair lets each thread write `*slot = count`; the borrow checker accepts it because no two threads hold the same `&mut`. When the scope returns, `totals` holds every result.
- Why is `move` written on the inner closure in `s.spawn(move || chunk.iter().count())`?It copies the `chunk` slice reference into the thread. On edition 2021 and later rustc also accepts the closure without `move`, because closures capture the place `*chunk` through a shared reborrow that can last as long as the slice. On edition 2018 the same code fails with E0373, since the closure would borrow the short-lived local `chunk` itself. Writing `move` works on every edition.
- What does `thread::scope` do if one of its threads panics and nobody joined it?It still waits for all the other scoped threads, then panics on the calling thread with the message "a scoped thread panicked". Joining a `ScopedJoinHandle` yourself returns `Err(payload)` instead and keeps the scope from panicking for that thread.
- Scoped threads are detached when the scope closure returns.
- thread::scope removes the borrow rules, so threads can all mutate the same Vec.
- Inside the scope the owner may still push to a Vec that workers are reading.
- Scoped threads must still use move and own their data.
- thread::scope returns immediately and the threads finish in the background.
Assumes Rust 1.99 on edition 2024; std::thread::scope has been stable since Rust 1.63, and older code used crossbeam's scoped threads instead.