1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-06-07 10:06:22 +02:00

Move thread::spawn to separate function (#1020)

This might make it clearer why the thread cannot borrow from the string.
This commit is contained in:
Martin Geisler 2023-07-24 23:28:14 +02:00 committed by GitHub
parent dbb3217667
commit 2c820e7bf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,13 +5,16 @@ Normal threads cannot borrow from their environment:
```rust,editable,compile_fail ```rust,editable,compile_fail
use std::thread; use std::thread;
fn main() { fn foo() {
let s = String::from("Hello"); let s = String::from("Hello");
thread::spawn(|| { thread::spawn(|| {
println!("Length: {}", s.len()); println!("Length: {}", s.len());
}); });
} }
fn main() {
foo();
}
``` ```
However, you can use a [scoped thread][1] for this: However, you can use a [scoped thread][1] for this: