@@ -205,33 +205,48 @@ The main drawback is that scoped threads make the standard library a little bit
205
205
# Rationale and alternatives
206
206
[ rationale-and-alternatives ] : #rationale-and-alternatives
207
207
208
- The alternative is to keep scoped threads in external crates. However, there are
209
- several advantages to having them in the standard library:
208
+ * Keep scoped threads in external crates.
210
209
211
- * This is a very common and useful utility and is great for learning, testing, and exploratory
212
- programming. Every person learning Rust will at some point encounter interaction
213
- of borrowing and threads. There's a very important lesson to be taught that threads
214
- * can* in fact borrow local variables, but the standard library doesn't reflect this.
210
+ There are several advantages to having them in the standard library:
215
211
216
- * Some might argue we should discourage using threads altogether and point people to
217
- executors like Rayon and Tokio instead. But still,
218
- the fact that ` thread::spawn() ` requires ` F: 'static ` and there's no way around it
219
- feels like a missing piece in the standard library.
212
+ * This is a very common and useful utility and is great for learning, testing, and exploratory
213
+ programming. Every person learning Rust will at some point encounter interaction
214
+ of borrowing and threads. There's a very important lesson to be taught that threads
215
+ * can * in fact borrow local variables, but the standard library doesn't reflect this .
220
216
221
- * Implementing scoped threads is very tricky to get right so it's good to have a
222
- reliable solution provided by the standard library.
217
+ * Some might argue we should discourage using threads altogether and point people to
218
+ executors like Rayon and Tokio instead. But still,
219
+ the fact that ` thread::spawn() ` requires ` F: 'static ` and there's no way around it
220
+ feels like a missing piece in the standard library.
223
221
224
- * There are many examples in the official documentation and books that could be
225
- simplified by scoped threads .
222
+ * Implementing scoped threads is very tricky to get right so it's good to have a
223
+ reliable solution provided by the standard library .
226
224
227
- * Scoped threads are typically a better default than ` thread::spawn() ` because
228
- they make sure spawned threads are joined and don't get accidentally "leaked".
229
- This is sometimes a problem in unit tests, where "dangling" threads can accumulate
230
- if unit tests spawn threads and forget to join them.
225
+ * There are many examples in the official documentation and books that could be
226
+ simplified by scoped threads.
227
+
228
+ * Scoped threads are typically a better default than ` thread::spawn() ` because
229
+ they make sure spawned threads are joined and don't get accidentally "leaked".
230
+ This is sometimes a problem in unit tests, where "dangling" threads can accumulate
231
+ if unit tests spawn threads and forget to join them.
232
+
233
+ * Users keep asking for scoped threads on IRC and forums
234
+ all the time. Having them as a "blessed" pattern in ` std::thread ` would be beneficial
235
+ to everyone.
236
+
237
+ * Return a ` Result ` from ` scope ` with all the captured panics.
238
+
239
+ * This quickly gets complicated, as multiple threads might have panicked.
240
+ Returning a ` Vec ` or other collection of panics isn't always the most useful interface,
241
+ and often unnecessary. Explicitly using ` .join() ` on the ` ScopedJoinHandle ` s to
242
+ handle panics is the most flexible and efficient way to handle panics, if the user wants
243
+ to handle them.
244
+
245
+ * Don't pass a ` &Scope ` argument to the threads.
246
+
247
+ * ` scope.spawn(|| ..) ` rather than ` scope.spawn(|scope| ..) ` would require the ` move ` keyword
248
+ (` scope.spawn(move || ..) ` ) if you want to use the scope inside that closure, which gets unergonomic.
231
249
232
- * Users keep asking for scoped threads on IRC and forums
233
- all the time. Having them as a "blessed" pattern in ` std::thread ` would be beneficial
234
- to everyone.
235
250
236
251
# Prior art
237
252
[ prior-art ] : #prior-art
0 commit comments