rust
rust copied to clipboard
'error: illegal borrow: borrowed value does not live long enough' on chained methods that return &'self pointers
trait Foo {
fn a(&self) -> &'self str;
fn b(&self) -> &'self str;
}
impl Foo for &'self str {
fn a(&self) -> &'self str {
*self
}
fn b(&self) -> &'self str {
*self
}
}
fn main () {
let c = "defg";
c.a();
c.b();
c.a().b(); // error: illegal borrow: borrowed value does not live long enough
}
Since I got an error message "Illegal lifetime 'self: the self lifetime must be declared" on several lines, I added declare like below and it doesn't occur any error now.
trait Foo<'self> {
fn a(&self) -> &'self str;
fn b(&self) -> &'self str;
}
impl<'self> Foo<'self> for &'self str {
fn a(&self) -> &'self str {
*self
}
fn b(&self) -> &'self str {
*self
}
}
fn main () {
let c = "defg";
c.a();
c.b();
c.a().b();
}
works on latest. seems no more issue.