Skip to content

Remove an uneeded 'static lifetime #1752

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

Merged
merged 5 commits into from
Dec 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion listings/ch12-an-io-project/listing-12-09/src/main.rs
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ struct Config {

// ANCHOR: here
impl Config {
fn new(args: &[String]) -> Result<Config, &'static str> {
fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
2 changes: 1 addition & 1 deletion listings/ch12-an-io-project/listing-12-10/src/main.rs
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ struct Config {
}

impl Config {
fn new(args: &[String]) -> Result<Config, &'static str> {
fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
2 changes: 1 addition & 1 deletion listings/ch12-an-io-project/listing-12-11/src/main.rs
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ struct Config {
}

impl Config {
fn new(args: &[String]) -> Result<Config, &'static str> {
fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
2 changes: 1 addition & 1 deletion listings/ch12-an-io-project/listing-12-12/src/main.rs
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@ struct Config {
}

impl Config {
fn new(args: &[String]) -> Result<Config, &'static str> {
fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
2 changes: 1 addition & 1 deletion listings/ch12-an-io-project/listing-12-13/src/lib.rs
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ pub struct Config {
}

impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
pub fn new(args: &[String]) -> Result<Config, &str> {
// --snip--
// ANCHOR_END: here
if args.len() < 3 {
2 changes: 1 addition & 1 deletion listings/ch12-an-io-project/listing-12-14/src/lib.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ pub struct Config {
}

impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
pub fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
2 changes: 1 addition & 1 deletion listings/ch12-an-io-project/listing-12-15/src/lib.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ pub struct Config {
}

impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
pub fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
2 changes: 1 addition & 1 deletion listings/ch12-an-io-project/listing-12-16/src/lib.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ pub struct Config {
}

impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
pub fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
2 changes: 1 addition & 1 deletion listings/ch12-an-io-project/listing-12-17/src/lib.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ pub struct Config {
}

impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
pub fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
2 changes: 1 addition & 1 deletion listings/ch12-an-io-project/listing-12-18/src/lib.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ pub struct Config {
}

impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
pub fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
2 changes: 1 addition & 1 deletion listings/ch12-an-io-project/listing-12-19/src/lib.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ pub struct Config {
}

impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
pub fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
2 changes: 1 addition & 1 deletion listings/ch12-an-io-project/listing-12-20/src/lib.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ pub struct Config {
}

impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
pub fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
2 changes: 1 addition & 1 deletion listings/ch12-an-io-project/listing-12-21/src/lib.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ pub struct Config {
}

impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
pub fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
2 changes: 1 addition & 1 deletion listings/ch12-an-io-project/listing-12-22/src/lib.rs
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ pub struct Config {
// ANCHOR_END: here

impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
pub fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
2 changes: 1 addition & 1 deletion listings/ch12-an-io-project/listing-12-23/src/lib.rs
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ pub struct Config {

// ANCHOR: here
impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
pub fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
2 changes: 1 addition & 1 deletion listings/ch12-an-io-project/listing-12-24/src/lib.rs
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ pub struct Config {
}

impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
pub fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@ struct Config {
}

impl Config {
fn new(args: &[String]) -> Result<Config, &'static str> {
fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ pub struct Config {
}

impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
pub fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ pub struct Config {
}

impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
pub fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ pub struct Config {
}

impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
pub fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ pub struct Config {
}

impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
pub fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ pub struct Config {

// ANCHOR: ch13
impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
pub fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ pub struct Config {
}

impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
pub fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
2 changes: 1 addition & 1 deletion listings/ch13-functional-features/listing-13-25/src/lib.rs
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ pub struct Config {
}

impl Config {
pub fn new(args: &[String]) -> Result<Config, &'static str> {
pub fn new(args: &[String]) -> Result<Config, &str> {
if args.len() < 3 {
return Err("not enough arguments");
}
8 changes: 2 additions & 6 deletions src/ch12-03-improving-error-handling-and-modularity.md
Original file line number Diff line number Diff line change
@@ -277,10 +277,7 @@ next listing.
`Config::new`</span>

Our `new` function now returns a `Result` with a `Config` instance in the
success case and a `&'static str` in the error case. Recall from [“The Static
Lifetime”][the-static-lifetime]<!-- ignore --> section in Chapter 10 that
`&'static str` is the type of string literals, which is our error message type
for now.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if we want to keep this sentence but remove the static lifetime bits. thoughts?

success case and a `&str` in the error case.

We’ve made two changes in the body of the `new` function: instead of calling
`panic!` when the user doesn’t pass enough arguments, we now return an `Err`
@@ -318,7 +315,7 @@ is an `Err` value, this method calls the code in the *closure*, which is an
anonymous function we define and pass as an argument to `unwrap_or_else`. We’ll
cover closures in more detail in [Chapter 13][ch13]<!-- ignore -->. For now,
you just need to know that `unwrap_or_else` will pass the inner value of the
`Err`, which in this case is the static string `not enough arguments` that we
`Err`, which in this case is the static string `"not enough arguments"` that we
added in Listing 12-9, to our closure in the argument `err` that appears
between the vertical pipes. The code in the closure can then use the `err`
value when it runs.
@@ -498,7 +495,6 @@ Let’s take advantage of this newfound modularity by doing something that would
have been difficult with the old code but is easy with the new code: we’ll
write some tests!

[the-static-lifetime]: ch10-03-lifetime-syntax.html#the-static-lifetime
[ch13]: ch13-00-functional-features.html
[ch9-custom-types]: ch09-03-to-panic-or-not-to-panic.html#creating-custom-types-for-validation
[ch9-error-guidelines]: ch09-03-to-panic-or-not-to-panic.html#guidelines-for-error-handling
12 changes: 12 additions & 0 deletions src/ch13-03-improving-our-io-project.md
Original file line number Diff line number Diff line change
@@ -89,6 +89,16 @@ signature of the `Config::new` function so the parameter `args` has the type
`args` and we’ll be mutating `args` by iterating over it, we can add the `mut`
keyword into the specification of the `args` parameter to make it mutable.

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”][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.

#### Using `Iterator` Trait Methods Instead of Indexing

Next, we’ll fix the body of `Config::new`. The standard library documentation
@@ -165,3 +175,5 @@ the iterator must pass.
But are the two implementations truly equivalent? The intuitive assumption
might be that the more low-level loop will be faster. Let’s talk about
performance.

[lifetime-elision]: ch10-03-lifetime-syntax.html#lifetime-elision