Skip to content

Conversation

oconnor663
Copy link
Contributor

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 
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
@rust-highfive
Copy link

r? @marioidival

(rust-highfive has picked a reviewer for you, use r? to override)

@marioidival marioidival merged commit 311f669 into rust-lang:master Aug 10, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Aug 17, 2022
Update books ## nomicon 2 commits in 8d1e4dccf71114ff56f328f671f2026d8e6b62a2..8e6aa3448515a0654e347b5e2510f1d4bc4d5a64 2022-07-18 18:12:35 -0400 to 2022-08-15 15:36:13 -0700 - Update the `repr(transparent)` section to reflect the current state (rust-lang/nomicon#376) - [typo] typo on limits of lifetime chapter (rust-lang/nomicon#377) ## reference 5 commits in f3d3953bf3b158d596c96d55ce5366f9f3f972e9..e647eb102890e8927f488bea12672b079eff8d9d 2022-08-01 17:17:37 -0700 to 2022-08-16 11:35:27 -0700 - #[non_exhaustive] on variant blocks cross-crate as casts (rust-lang/reference#1249) - Revert let chains reference docs (rust-lang/reference#1251) - Update subtyping.md (rust-lang/reference#1240) - a fix about .await (rust-lang/reference#1245) - Be less specific about the representation of `+bundle` (rust-lang/reference#1246) ## book 2 commits in 36383b4da21dbd0a0781473bc8ad7ef0ed1b6751..42ca0ef484fcc8437a0682cee23abe4b7c407d52 2022-07-19 21:03:20 -0400 to 2022-08-12 21:52:02 -0400 - Missing period at end of sentence - Fix grammar in ch06-02 ## rust-by-example 8 commits in ee342dc91e1ba1bb1e1f1318f84bbe3bfac04798..03301f8ae55fa6f20f7ea152a517598e6db2cdb7 2022-07-27 11:06:36 -0300 to 2022-08-14 08:51:44 -0300 - Update print.md (rust-lang/rust-by-example#1597) - in Meta, replace 'playpen' with 'playground' (rust-lang/rust-by-example#1596) - Update doc comment to link to name field without compilation warning (rust-lang/rust-by-example#1595) - add line numbers for playpen fixes rust-lang/rust-by-example#1593 (rust-lang/rust-by-example#1594) - clarify that the map-reduce example relies on static data (rust-lang/rust-by-example#1592) - Update flow_control.md (rust-lang/rust-by-example#1591) - Remove duplicate line in the hello/print.md file (rust-lang/rust-by-example#1590) - Make rust editable in chapter on defaults (rust-lang/rust-by-example#1589) ## rustc-dev-guide 15 commits in 04f3cf0..d3daa1f 2022-07-31 07:46:57 +0200 to 2022-08-13 10:00:38 +0900 - Improve the "Diagnostic items" chapter (rust-lang/rustc-dev-guide#1427) - date-check: crates-io - fix/improve compiler-debugging - Update src/compiler-debugging.md - add gdb tips for symbol-mangling-version - move references down to avoid clutter (rust-lang/rustc-dev-guide#1420) - update date-check format on github issue (rust-lang/rustc-dev-guide#1416) - Fix legend colors in dark mode - Add color for downloaded nodes - Add colors to diagram - Add bootstrapping diagram - date-check: rustc_codegen_ssa is still alive - note is now too old to be relevant - date-check: be more strict - make date-check more lightweight (rust-lang/rustc-dev-guide#1394) ## edition-guide 3 commits in c55611dd6c58bdeb52423b5c52fd0f3c93615ba8..6038be9d37d7251c966b486154af621d1794d7af 2022-02-21 14:21:39 +0100 to 2022-08-15 08:12:42 -0700 - use title "The Rust Edition Guide" everywhere (rust-lang/edition-guide#280) - "Creating a new project": add example using 'cargo new --edition YEAR' (rust-lang/edition-guide#279) - fixes rust-lang/edition-guide#277: mention rust 1.0 release month and year (rust-lang/edition-guide#278)
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Aug 17, 2022
Update books ## nomicon 2 commits in 8d1e4dccf71114ff56f328f671f2026d8e6b62a2..8e6aa3448515a0654e347b5e2510f1d4bc4d5a64 2022-07-18 18:12:35 -0400 to 2022-08-15 15:36:13 -0700 - Update the `repr(transparent)` section to reflect the current state (rust-lang/nomicon#376) - [typo] typo on limits of lifetime chapter (rust-lang/nomicon#377) ## reference 5 commits in f3d3953bf3b158d596c96d55ce5366f9f3f972e9..e647eb102890e8927f488bea12672b079eff8d9d 2022-08-01 17:17:37 -0700 to 2022-08-16 11:35:27 -0700 - #[non_exhaustive] on variant blocks cross-crate as casts (rust-lang/reference#1249) - Revert let chains reference docs (rust-lang/reference#1251) - Update subtyping.md (rust-lang/reference#1240) - a fix about .await (rust-lang/reference#1245) - Be less specific about the representation of `+bundle` (rust-lang/reference#1246) ## book 2 commits in 36383b4da21dbd0a0781473bc8ad7ef0ed1b6751..42ca0ef484fcc8437a0682cee23abe4b7c407d52 2022-07-19 21:03:20 -0400 to 2022-08-12 21:52:02 -0400 - Missing period at end of sentence - Fix grammar in ch06-02 ## rust-by-example 8 commits in ee342dc91e1ba1bb1e1f1318f84bbe3bfac04798..03301f8ae55fa6f20f7ea152a517598e6db2cdb7 2022-07-27 11:06:36 -0300 to 2022-08-14 08:51:44 -0300 - Update print.md (rust-lang/rust-by-example#1597) - in Meta, replace 'playpen' with 'playground' (rust-lang/rust-by-example#1596) - Update doc comment to link to name field without compilation warning (rust-lang/rust-by-example#1595) - add line numbers for playpen fixes rust-lang/rust-by-example#1593 (rust-lang/rust-by-example#1594) - clarify that the map-reduce example relies on static data (rust-lang/rust-by-example#1592) - Update flow_control.md (rust-lang/rust-by-example#1591) - Remove duplicate line in the hello/print.md file (rust-lang/rust-by-example#1590) - Make rust editable in chapter on defaults (rust-lang/rust-by-example#1589) ## rustc-dev-guide 15 commits in 04f3cf0..d3daa1f 2022-07-31 07:46:57 +0200 to 2022-08-13 10:00:38 +0900 - Improve the "Diagnostic items" chapter (rust-lang/rustc-dev-guide#1427) - date-check: crates-io - fix/improve compiler-debugging - Update src/compiler-debugging.md - add gdb tips for symbol-mangling-version - move references down to avoid clutter (rust-lang/rustc-dev-guide#1420) - update date-check format on github issue (rust-lang/rustc-dev-guide#1416) - Fix legend colors in dark mode - Add color for downloaded nodes - Add colors to diagram - Add bootstrapping diagram - date-check: rustc_codegen_ssa is still alive - note is now too old to be relevant - date-check: be more strict - make date-check more lightweight (rust-lang/rustc-dev-guide#1394) ## edition-guide 3 commits in c55611dd6c58bdeb52423b5c52fd0f3c93615ba8..6038be9d37d7251c966b486154af621d1794d7af 2022-02-21 14:21:39 +0100 to 2022-08-15 08:12:42 -0700 - use title "The Rust Edition Guide" everywhere (rust-lang/edition-guide#280) - "Creating a new project": add example using 'cargo new --edition YEAR' (rust-lang/edition-guide#279) - fixes rust-lang/edition-guide#277: mention rust 1.0 release month and year (rust-lang/edition-guide#278)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants