Skip to content

Commit

Permalink
Merge pull request rust-lang#1752 from rust-lang/gh1744
Browse files Browse the repository at this point in the history
Remove an uneeded 'static lifetime
  • Loading branch information
steveklabnik authored Dec 14, 2020
2 parents b4e5fcc + 4275162 commit b702f3b
Show file tree
Hide file tree
Showing 26 changed files with 38 additions and 30 deletions.
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
Expand Up @@ -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");
}
Expand Down
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
Expand Up @@ -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");
}
Expand Down
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
Expand Up @@ -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");
}
Expand Down
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
Expand Up @@ -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");
}
Expand Down
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
Expand Up @@ -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 {
Expand Down
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
Expand Up @@ -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");
}
Expand Down
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
Expand Up @@ -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");
}
Expand Down
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
Expand Up @@ -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");
}
Expand Down
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
Expand Up @@ -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");
}
Expand Down
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
Expand Up @@ -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");
}
Expand Down
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
Expand Up @@ -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");
}
Expand Down
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
Expand Up @@ -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");
}
Expand Down
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
Expand Up @@ -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");
}
Expand Down
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
Expand Up @@ -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");
}
Expand Down
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
Expand Up @@ -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");
}
Expand Down
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
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
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
Expand Up @@ -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");
}
Expand Down
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
Expand Up @@ -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.
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`
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/ch13-03-improving-our-io-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit b702f3b

Please sign in to comment.