Skip to content

Chapter 10.3. Third elision rule. #3386

@smccrossin

Description

@smccrossin
  • I have checked the latest main branch to see if this has already been fixed
  • I have searched existing issues and pull requests for duplicates

URL to the section(s) of the book with this problem:
https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html

Description of the problem:
This has to do with the example provided of the 3rd elision rule in action:

impl<'a> ImportantExcerpt<'a> {
    fn announce_and_return_part(&self, announcement: &str) -> &str {
        println!("Attention please: {}", announcement);
        self.part
    }
}

I'm new to Rust. My immediate thought in reading this example was "Oh no, what if the function returns 'announcement'? Assigning self's lifetime to the output reference seems very dangerous here. What if 'announcement' is returned instead?" I was so concerned, I tried it. And was very relieved that the compiler reported an error.

Suggested fix:
I would suggest stating that the 3rd elision is making the assumption that self (or some component thereof) will be returned. And although the compiler will in all cases apply the 3rd elision rule to a method signature like the above, the code will not compile if that assumption is violated. This is an important difference from the first two elision rules. I believe they can safely be applied without any particular assumptions about the body of the function. Otherwise put, I don't think either of them could cause code to fail to compile.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions