Skip to content

"Improperly reduced borrows" can be updated soon. #527

Description

@petercs-sophos

https://doc.rust-lang.org/nomicon/lifetime-mismatch.html#improperly-reduced-borrows states the code below will not compile:

fn get_default<'m, K, V>(map: &'m mut HashMap<K, V>, key: K) -> &'m mut V
where
    K: Clone + Eq + Hash,
    V: Default,
{
    match map.get_mut(&key) {
        Some(value) => value,
        None => {
            map.insert(key.clone(), V::default());
            map.get_mut(&key).unwrap()
        }
    }
}

However, on nightly it will now compile due to Polonius Alpha being enabled (https://blog.rust-lang.org/2026/08/04/enabling-polonius-alpha-on-nightly/), it may be worth mentioning this.

Additionally, the name of the function should probably be get_mut_or_default as it is in the article.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions