Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 12.3: Incorrect lifetime implied in signature of Config::new #2864

Closed
2 tasks done
jruderman opened this issue Sep 9, 2021 · 0 comments · Fixed by rust-lang/rust#92546
Closed
2 tasks done
Milestone

Comments

@jruderman
Copy link
Contributor

jruderman commented Sep 9, 2021

  • I have checked the latest main branch to see if this has already been fixed
  • I have searched existing issues and pull requests for duplicates

URL to the section(s) of the book with this problem:

https://doc.rust-lang.org/book/ch12-03-improving-error-handling-and-modularity.html

Description of the problem:

fn new(args: &[String]) -> Result<Config, &str> { ... }

Due to lifetime elision rules, this ties the lifetime of the &str in the Result to the lifetime of args. It happens to compile with the main function given in listing 12-10, but this just seems like a lucky coincidence. An alternative main function would encounter an error due to the implied lifetime relationship:

fn main() {
    let config = Config::new(&env::args().collect::<Vec<String>>());

    let config = config.unwrap_or_else(|err| {
        println!("Problem parsing arguments: {}", err);
        process::exit(1);
    });
}

Suggested fix:

  • Starting in listing 12-9, and in subsequent copies before listing 13-26, add a 'static lifetime to the output str:
    • fn new(args: &[String]) -> Result<Config, &'static str> { ... }
  • Maybe add a brief explanation below listing 12-9 of why specifying 'static makes the signature better reflect what the function does.
  • After listing 13-26, remove the following paragraph:

We also needed to specify that the string slice error type can now only have the 'static lifetime. Because we’re only ever returning string literals, this was true before. However, when we had a reference in the parameters, there was the possibility that the reference in the return type could have had the same lifetime as the reference in the parameters. The rules that we discussed in the “Lifetime Elision” section of Chapter 10 applied, and we weren’t required to annotate the lifetime of &str. With the change to args, the lifetime elision rules no longer apply, and we must specify the 'static lifetime.

@carols10cents carols10cents added this to the ch12 milestone Sep 17, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 4, 2022
Update books

## reference

3 commits in 06f9e61931bcf58b91dfe6c924057e42ce273ee1..f8ba2f12df60ee19b96de24ae5b73af3de8a446b
2021-12-17 07:31:40 -0800 to 2022-01-03 11:02:08 -0800
- Switch the default edition for examples to 2021 (rust-lang/reference#1125)
- Clarify behavior of x87 FP registers in inline assembly (rust-lang/reference#1126)
- Add inline assembly to the reference (rust-lang/reference#1105)

## book

36 commits in 8a0bb3c96e71927b80fa2286d7a5a5f2547c6aa4..d3740fb7aad0ea4a80ae20f64dee3a8cfc0c5c3c
2021-12-22 20:54:27 -0500 to 2022-01-03 21:46:04 -0500
- Add a concrete example of an optional value. Fixes rust-lang/book#2848.
- match isn't really an operator. Fixes rust-lang/book#2859
- Edits to edits of chapter 6
- Make fixes recommended by shellcheck
- Use shellcheck
- SIGH fix all the typos that were missed while spellcheck was broken
- SIGH add all the words to the dictionary that were missed while spellcheck was broken
- Remove test_harness from the dictionary
- sigh, the xkcd sandwich one
- Install aspell in CI
- set -eu in all bash scripts
- typo: assignement -&gt; assignment
- Fix quotes
- Snapshot of ch12 for nostarch
- Use 'static lifetime earlier because that's more correct. Fixes rust-lang/book#2864.
- Add does_not_compile annotation to intermediate steps that don't compile
- Sidestep who provides output streams. Fixes rust-lang/book#2933.
- Remove note about primitive obsession. Fixes rust-lang/book#2863.
- Remove sentence encouraging writing tests on your own. Fixes rust-lang/book#2223.
- Bump mdBook version to 0.4.14 in workflow main.yml
- Past tense make better sense
- Past tense makes better sense
- Update the edition in all the listings' Cargo.toml
- Update the book to either say 2021 edition or not talk about editions
- Remove most of the 2018 edition text from the title page. Fixes rust-lang/book#2852.
- Fix word wrapping
- Emphasize return type is mandatory
- fix title capitalization
- Further edits to mention of --include-ignored, propagate to src
- feat: mention `cargo test -- --include-ignored`
- wording: get rid of "to from"
- interchanged position of `binary` and `library`
- Fix wrong word typo
- Further edits in rust-analyzer text
- appendix-04 IDE integration: Replaced rls by rust-analyzer
- Update link to Italian translation. Connects to rust-lang/book#2484.

## rustc-dev-guide

3 commits in 9bf0028b557798ddd07a6f652e4d0c635d3d6620..875464457c4104686faf667f47848aa7b0f0a744
2021-12-20 21:53:57 +0900 to 2021-12-28 22:17:49 -0600
- Update link to moved section (rust-lang/rustc-dev-guide#1282)
- Fix link in contributing.md (rust-lang/rustc-dev-guide#1280)
- Streamline "Getting Started" (rust-lang/rustc-dev-guide#1279)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants