From 2c820e7bf322953114b6b15c40e9267dbcf84138 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Mon, 24 Jul 2023 23:28:14 +0200 Subject: [PATCH] Move `thread::spawn` to separate function (#1020) This might make it clearer why the thread cannot borrow from the string. --- src/concurrency/scoped-threads.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/concurrency/scoped-threads.md b/src/concurrency/scoped-threads.md index 1f58a828..95680e06 100644 --- a/src/concurrency/scoped-threads.md +++ b/src/concurrency/scoped-threads.md @@ -5,13 +5,16 @@ Normal threads cannot borrow from their environment: ```rust,editable,compile_fail use std::thread; -fn main() { +fn foo() { let s = String::from("Hello"); - thread::spawn(|| { println!("Length: {}", s.len()); }); } + +fn main() { + foo(); +} ``` However, you can use a [scoped thread][1] for this: