Skip to content

'error: illegal borrow: borrowed value does not live long enough' on chained methods that return &'self pointers #5475

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

Closed
Kimundi opened this issue Mar 21, 2013 · 2 comments

Comments

@Kimundi
Copy link
Member

Kimundi commented Mar 21, 2013

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
}
@june0cho
Copy link
Contributor

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();
}

@yichoi
Copy link
Contributor

yichoi commented May 16, 2013

works on latest. seems no more issue.

@yichoi yichoi closed this as completed May 16, 2013
Sawyer47 added a commit to Sawyer47/rust that referenced this issue May 25, 2014
Issue rust-lang#5475 was closed some time ago, but ascii.rs still contained
a FIXME for it.
bors added a commit that referenced this issue May 25, 2014
Issue #5475 was closed some time ago, but ascii.rs still contained
a FIXME for it.
oli-obk pushed a commit to oli-obk/rust that referenced this issue May 2, 2020
[fix] Minor typo in GH Actions 'clippy_dev'

changelog: minor: fixes file name in Github Actions workflow `clippy_dev.yml`

I was looking for something else and ended up on that typo, just thought I would fix it.
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

No branches or pull requests

3 participants