Skip to content

Commit 9e90f8d

Browse files
authored
Rollup merge of #94763 - m-ou-se:scoped-threads-lifetime-docs, r=Mark-Simulacrum
Add documentation about lifetimes to thread::scope. This resolves the last unresolved question of #93203 This was brought up in #94559 (comment) r? `````@Mark-Simulacrum`````
2 parents 99c6be1 + 4d56c15 commit 9e90f8d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

library/std/src/thread/scoped.rs

+18
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,24 @@ impl ScopeData {
107107
/// a.push(4);
108108
/// assert_eq!(x, a.len());
109109
/// ```
110+
///
111+
/// # Lifetimes
112+
///
113+
/// Scoped threads involve two lifetimes: `'scope` and `'env`.
114+
///
115+
/// The `'scope` lifetime represents the lifetime of the scope itself.
116+
/// That is: the time during which new scoped threads may be spawned,
117+
/// and also the time during which they might still be running.
118+
/// Once this lifetime ends, all scoped threads are joined.
119+
/// This lifetime starts within the `scope` function, before `f` (the argument to `scope`) starts.
120+
/// It ends after `f` returns and all scoped threads have been joined, but before `scope` returns.
121+
///
122+
/// The `'env` lifetime represents the lifetime of whatever is borrowed by the scoped threads.
123+
/// This lifetime must outlast the call to `scope`, and thus cannot be smaller than `'scope`.
124+
/// It can be as small as the call to `scope`, meaning that anything that outlives this call,
125+
/// such as local variables defined right before the scope, can be borrowed by the scoped threads.
126+
///
127+
/// The `'env: 'scope` bound is part of the definition of the `Scope` type.
110128
#[track_caller]
111129
pub fn scope<'env, F, T>(f: F) -> T
112130
where

0 commit comments

Comments
 (0)