Skip to content

Unexpected borrowcheck error when using Cow<> #82146

@emilio

Description

@emilio

It seems to me this ought to compile, or at least it's not clear to me why it doesn't. The following code:

use std::collections::HashMap;
use std::borrow::Cow;

pub type Cache = HashMap<i32, i32>;

fn compute<'cache>(input: i32, cache: &'cache mut Cache) -> Cow<'cache, i32> {
    if let Some(cached) = cache.get(&input) {
        return Cow::Borrowed(cached)
    }
    
    let result = input + 2;
    cache.insert(input, result);
    Cow::Borrowed(&cache[&input])
}

fn main() {}

Yields:

error[E0502]: cannot borrow `*cache` as mutable because it is also borrowed as immutable
  --> src/lib.rs:12:5
   |
6  | fn compute<'cache>(input: i32, cache: &'cache mut Cache) -> Cow<'cache, i32> {
   |            ------ lifetime `'cache` defined here
7  |     if let Some(cached) = cache.get(&input) {
   |                           ----- immutable borrow occurs here
8  |         return Cow::Borrowed(cached);
   |                --------------------- returning this value requires that `*cache` is borrowed for `'cache`
...
12 |     cache.insert(input, result);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here

error: aborting due to previous error

For more information about this error, try `rustc --explain E0502`.

Obviously my actual code is more complicated than this (it computes multiple outputs in the same non-cached path, might not cache the input, etc), so using .entry() etc is not really an option.

What am I missing?

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