Lifetime question: is my intuition correct?

⚓ Rust    📅 2026-02-16    👤 surdeus    👁️ 5      

surdeus

struct Person<'a> {
    given: &'a str,
    sur: &'a mut str,
}

impl<'a> Person<'a> {
    fn given(&'a self) -> &'a str {
        self.given
    }

    fn sur_mut(&'a mut self) -> &'a mut str {
        self.sur
    }
}

fn example<'a>(mut person: Person<'a>) {
    let _one = person.given();
    let _two = person.given();
    let _three = person.sur_mut(); 
}

Hello folks, I was tweaking and trying out some different things with an example given here: quinedot rust learning guide
I understand that &'a mut &'a _ should be avoided and is pretty restrictive but, I feel like the problem isn't about that...

from what I understand this line

let _three = person.sur_mut(); 

is a reborrow of the mutable reference with a shorter lifetime, right?
but the function is annotated with a lifetime 'a and therefore expects the borrow from the struct to live as long 'a, is my intuition correct?

4 posts - 4 participants

Read full topic

🏷️ Rust_feed