Skip to content

Commit adca786

Browse files
authored
Merge pull request #254 from mdaverde/ml/adds-compiler-err-lifetimes
2 parents bbf06ad + 8565958 commit adca786

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/lifetime-mismatch.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ care about, but the lifetime system is too coarse-grained to handle that.
7474

7575
# Improperly reduced borrows
7676

77-
This currently fails to compile, because Rust doesn't understand that the borrow
77+
The following code fails to compile, because Rust doesn't understand that the borrow
7878
is no longer needed and conservatively falls back to using a whole scope for it.
7979
This will eventually get fixed.
8080

@@ -96,5 +96,29 @@ where
9696
}
9797
```
9898

99+
Because of the lifetime restrictions imposed, `&mut map`'s lifetime
100+
overlaps other mutable borrows, resulting in a compile error:
101+
102+
```text
103+
error[E0499]: cannot borrow `*map` as mutable more than once at a time
104+
--> src/main.rs:12:13
105+
|
106+
4 | fn get_default<'m, K, V>(map: &'m mut HashMap<K, V>, key: K) -> &'m mut V
107+
| -- lifetime `'m` defined here
108+
...
109+
9 | match map.get_mut(&key) {
110+
| - --- first mutable borrow occurs here
111+
| _____|
112+
| |
113+
10 | | Some(value) => value,
114+
11 | | None => {
115+
12 | | map.insert(key.clone(), V::default());
116+
| | ^^^ second mutable borrow occurs here
117+
13 | | map.get_mut(&key).unwrap()
118+
14 | | }
119+
15 | | }
120+
| |_____- returning this value requires that `*map` is borrowed for `'m`
121+
```
122+
99123

100124
[ex2]: lifetimes.html#example-aliasing-a-mutable-reference

0 commit comments

Comments
 (0)