Immutable borrow from mutable self prevents further immutable borrow of self

The two borrowed types in this function signature have the same lifetime, which we can see more clearly if we apply the lifetime elision rules:

pub fn get_or_create<'a>(&'a mut self, key: String) -> &'a str

This means that the &mut self borrow passed in must live as long as the &str value that gets returned.

There's currently no way to say "this function borrows self mutably/exclusively for one lifetime, and immutably/shared for a different lifetime." This is a known limitation of lifetime syntax.

5 Likes