Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clarify that the map-reduce example relies on static data (#1592)
The map-reduce example currently says "Because we're `move`-ing the data segments into the thread, Rust will also ensure the data is kept alive until the threads exit." Both halves of that sentence are true by themselves, but I think the "because" is misleading. The main reason Rust isn't giving us lifetime errors here is that the original `data` string is a `&'static str` constant. The `move` keyword is important for _keeping_ the data static (by capturing `&str` instead of `&&str`), but it's not what _made_ the data static. If you force the data to be non-static, like by adding `.to_string()` to the end of it, you can see that even with `move` this example doesn't compile: error[E0597]: `data` does not live long enough --> src/main.rs:31:24 | 31 | let chunked_data = data.split_whitespace(); | ^^^^^^^^^^^^^^^^^^^^^^^ borrowed value does not live long enough ... 55 | children.push(thread::spawn(move || -> u32 { | _______________________- 56 | | // Calculate the intermediate sum of this segment: 57 | | let result = data_segment 58 | | // iterate over the characters of our segment.. ... | 70 | | result 71 | | })); | |__________- argument requires that `data` is borrowed for `'static` ... 89 | } | - `data` dropped here while still borrowed
- Loading branch information