1 definition by x1unix

Rust is an ancient African word, meaning "I can't learn C++"

Also known for a borrow-checker which makes soydevs crazy.
It seems to me this ought to compile, or at least it's not clear to me why it doesn't. The following code yields:

errorE0502: 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`.
by x1unix August 6, 2021
Get the Rust mug.