Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
pedantic
Source Link
Shepmaster
  • 439.4k
  • 116
  • 1.3k
  • 1.5k

Short answer: remove the 'a lifetime for the self parameter of split_first: fn split_first(&self) -> &'a str (playground).

Long answer:

When you write this code:

struct Foo<'a> { part: &'a str, } impl<'a> Foo<'a> { fn new(s: &'a str) -> Self { Foo { part: s } } } 

You are telling the compiler that all Foo instances are related to some lifetime 'a that must be equal to or shorter than the lifetime of the string passed as parameter to Foo::new. That lifetime 'a may be different from the lifetime of each Foo instance. When you then write:

let words = "Sometimes think, the greatest sorrow than older"; Foo::new(words) 

The compiler infers that the lifetime 'a must be equal to or shorter than the lifetime of words. Barring any other constraints the compiler will use the lifetime of words, which is 'static so it is valid for the full life of the program.

When you add your definition of split_first:

fn split_first(&'a self) -> &'a str 

You are adding an extra constraint: you are saying that 'a must also be equal to or shorter than the lifetime of self. The compiler will therefore take the shorter of the two lifetimes (the lifetime of words and the lifetime of the temporary Foo instance), which is the lifetime of the temporary. @AndersKaseorg's answer@AndersKaseorg's answer explains why that doesn't work.

By removing the 'a lifetime on the self parameter, I am decorrelating 'a from the lifetime of the temporary, so the compiler can again infer that 'a is the lifetime of words, which is long enough for the program to work.

Short answer: remove the 'a lifetime for the self parameter of split_first: fn split_first(&self) -> &'a str (playground).

Long answer:

When you write this code:

struct Foo<'a> { part: &'a str, } impl<'a> Foo<'a> { fn new(s: &'a str) -> Self { Foo { part: s } } } 

You are telling the compiler that all Foo instances are related to some lifetime 'a that must be shorter than the lifetime of the string passed as parameter to Foo::new. That lifetime 'a may be different from the lifetime of each Foo instance. When you then write:

let words = "Sometimes think, the greatest sorrow than older"; Foo::new(words) 

The compiler infers that the lifetime 'a must be shorter than the lifetime of words. Barring any other constraints the compiler will use the lifetime of words, which is 'static so it is valid for the full life of the program.

When you add your definition of split_first:

fn split_first(&'a self) -> &'a str 

You are adding an extra constraint: you are saying that 'a must be shorter than the lifetime of self. The compiler will therefore take the shorter of the two lifetimes (the lifetime of words and the lifetime of the temporary Foo instance) which is the lifetime of the temporary. @AndersKaseorg's answer explains why that doesn't work.

By removing the 'a lifetime on the self parameter, I am decorrelating 'a from the lifetime of the temporary, so the compiler can again infer that 'a is the lifetime of words, which is long enough for the program to work.

Short answer: remove the 'a lifetime for the self parameter of split_first: fn split_first(&self) -> &'a str (playground).

Long answer:

When you write this code:

struct Foo<'a> { part: &'a str, } impl<'a> Foo<'a> { fn new(s: &'a str) -> Self { Foo { part: s } } } 

You are telling the compiler that all Foo instances are related to some lifetime 'a that must be equal to or shorter than the lifetime of the string passed as parameter to Foo::new. That lifetime 'a may be different from the lifetime of each Foo instance. When you then write:

let words = "Sometimes think, the greatest sorrow than older"; Foo::new(words) 

The compiler infers that the lifetime 'a must be equal to or shorter than the lifetime of words. Barring any other constraints the compiler will use the lifetime of words, which is 'static so it is valid for the full life of the program.

When you add your definition of split_first:

fn split_first(&'a self) -> &'a str 

You are adding an extra constraint: you are saying that 'a must also be equal to or shorter than the lifetime of self. The compiler will therefore take the shorter of the lifetime of words and the lifetime of the temporary Foo instance, which is the lifetime of the temporary. @AndersKaseorg's answer explains why that doesn't work.

By removing the 'a lifetime on the self parameter, I am decorrelating 'a from the lifetime of the temporary, so the compiler can again infer that 'a is the lifetime of words, which is long enough for the program to work.

deleted 1 character in body
Source Link
Shepmaster
  • 439.4k
  • 116
  • 1.3k
  • 1.5k

Short answer: remove the 'a lifetime for the self parameter of split_first: fn split_first(&self) -> &'a str (playground).

Long answer:

When you write this code:

struct Foo<'a> { part: &'a str, } impl<'a> Foo<'a> { fn new(s: &'a str) -> Self { Foo { part: s } } } 

You are telling the compiler that all Foo instances are related to some lifetime 'a that must be shorter than the lifetime of the string passed as parameter to Foo::new. That lifetime 'a may be different from the lifetime of each Foo instance. When you then write:

let words = "Sometimes think, the greatest sorrow than older"; Foo::new (words) 

The compiler infers that the lifetime 'a must be shorter than the lifetime of words. Barring any other constraints the compiler will use the lifetime of words, which is 'static so it is valid for the full life of the program.

When you add your definition of split_first:

fn split_first(&'a self) -> &'a str 

You are adding an extra constraint: you are saying that 'a must be shorter than the lifetime of self. The compiler will therefore take the shorter of the two lifetimes (the lifetime of words and the lifetime of the temporary Foo instance) which is the lifetime of the temporary. @AndersKaseorg's answer explains why that doesn't work.

By removing the 'a lifetime on the self parameter, I am decorrelating 'a from the lifetime of the temporary, so the compiler can again infer that 'a is the lifetime of words, which is long enough for the program to work.

Short answer: remove the 'a lifetime for the self parameter of split_first: fn split_first(&self) -> &'a str (playground).

Long answer:

When you write this code:

struct Foo<'a> { part: &'a str, } impl<'a> Foo<'a> { fn new(s: &'a str) -> Self { Foo { part: s } } } 

You are telling the compiler that all Foo instances are related to some lifetime 'a that must be shorter than the lifetime of the string passed as parameter to Foo::new. That lifetime 'a may be different from the lifetime of each Foo instance. When you then write:

let words = "Sometimes think, the greatest sorrow than older"; Foo::new (words) 

The compiler infers that the lifetime 'a must be shorter than the lifetime of words. Barring any other constraints the compiler will use the lifetime of words, which is 'static so it is valid for the full life of the program.

When you add your definition of split_first:

fn split_first(&'a self) -> &'a str 

You are adding an extra constraint: you are saying that 'a must be shorter than the lifetime of self. The compiler will therefore take the shorter of the two lifetimes (the lifetime of words and the lifetime of the temporary Foo instance) which is the lifetime of the temporary. @AndersKaseorg's answer explains why that doesn't work.

By removing the 'a lifetime on the self parameter, I am decorrelating 'a from the lifetime of the temporary, so the compiler can again infer that 'a is the lifetime of words, which is long enough for the program to work.

Short answer: remove the 'a lifetime for the self parameter of split_first: fn split_first(&self) -> &'a str (playground).

Long answer:

When you write this code:

struct Foo<'a> { part: &'a str, } impl<'a> Foo<'a> { fn new(s: &'a str) -> Self { Foo { part: s } } } 

You are telling the compiler that all Foo instances are related to some lifetime 'a that must be shorter than the lifetime of the string passed as parameter to Foo::new. That lifetime 'a may be different from the lifetime of each Foo instance. When you then write:

let words = "Sometimes think, the greatest sorrow than older"; Foo::new(words) 

The compiler infers that the lifetime 'a must be shorter than the lifetime of words. Barring any other constraints the compiler will use the lifetime of words, which is 'static so it is valid for the full life of the program.

When you add your definition of split_first:

fn split_first(&'a self) -> &'a str 

You are adding an extra constraint: you are saying that 'a must be shorter than the lifetime of self. The compiler will therefore take the shorter of the two lifetimes (the lifetime of words and the lifetime of the temporary Foo instance) which is the lifetime of the temporary. @AndersKaseorg's answer explains why that doesn't work.

By removing the 'a lifetime on the self parameter, I am decorrelating 'a from the lifetime of the temporary, so the compiler can again infer that 'a is the lifetime of words, which is long enough for the program to work.

Source Link
Jmb
  • 24.2k
  • 2
  • 39
  • 70

Short answer: remove the 'a lifetime for the self parameter of split_first: fn split_first(&self) -> &'a str (playground).

Long answer:

When you write this code:

struct Foo<'a> { part: &'a str, } impl<'a> Foo<'a> { fn new(s: &'a str) -> Self { Foo { part: s } } } 

You are telling the compiler that all Foo instances are related to some lifetime 'a that must be shorter than the lifetime of the string passed as parameter to Foo::new. That lifetime 'a may be different from the lifetime of each Foo instance. When you then write:

let words = "Sometimes think, the greatest sorrow than older"; Foo::new (words) 

The compiler infers that the lifetime 'a must be shorter than the lifetime of words. Barring any other constraints the compiler will use the lifetime of words, which is 'static so it is valid for the full life of the program.

When you add your definition of split_first:

fn split_first(&'a self) -> &'a str 

You are adding an extra constraint: you are saying that 'a must be shorter than the lifetime of self. The compiler will therefore take the shorter of the two lifetimes (the lifetime of words and the lifetime of the temporary Foo instance) which is the lifetime of the temporary. @AndersKaseorg's answer explains why that doesn't work.

By removing the 'a lifetime on the self parameter, I am decorrelating 'a from the lifetime of the temporary, so the compiler can again infer that 'a is the lifetime of words, which is long enough for the program to work.