Last active
December 26, 2018 08:34
-
-
Save loliGothicK/912a726261a29b012d24c5ce97987d88 to your computer and use it in GitHub Desktop.
Non-Lexical Lifetimes ref: https://qiita.com/_EnumHack/items/8b6ecdeb52e69a4ff384
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let mut scores = vec![1, 2, 3]; // --+ 'scope | |
let score = &scores[0]; // | | |
// ^~~~~~~~~~~~~~~~~~~~~ 'lifetime // | | |
println!("{}", score); // | | |
scores.push(4); // | | |
} // <-----------------------------------+ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let mut scores = vec![1, 2, 3]; // -------------+ 'scope | |
{ // | | |
let score = &scores[0]; // <-+ 'lifetime | | |
println!("{}", score); // | | | |
} // <---------------------------+ | | |
scores.push(4); // OK! | | |
} // <----------------------------------------------+ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
error[E0597]: `n` does not live long enough | |
--> src/main.rs:11:20 | |
| | |
6 | fn to_refs<T>(mut list: &mut List<T>) -> Vec<&mut T> { | |
| - let's call the lifetime of this reference `'1` | |
... | |
11 | list = &mut n; | |
| -------^^^^^^ | |
| | | | |
| | borrowed value does not live long enough | |
| assignment requires that `n` is borrowed for `'1` | |
... | |
14 | } | |
| - `n` dropped here while still borrowed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn process_or_default() { | |
let mut map = ...; | |
let key = ...; | |
match map.get_mut(&key) { // -------------+ 'lifetime | |
Some(value) => process(value), // | | |
None => { // | | |
map.insert(key, V::default()); // | | |
// ^~~~~~ ERROR. // | | |
} // | | |
} // <------------------------------------+ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn process_or_default1() { | |
let mut map = ...; | |
let key = ...; | |
match map.get_mut(&key) { // -------------+ 'lifetime | |
Some(value) => { // | | |
process(value); // | | |
return; // | | |
} // | | |
None => { // | | |
} // | | |
} // <------------------------------------+ | |
map.insert(key, V::default()); // OK! | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
fn get_default<'r,K,V:Default>(map: &'r mut HashMap<K,V>, | |
key: K) | |
-> &'r mut V | |
where K: std::cmp::Eq + std::hash::Hash + Copy | |
{ | |
match map.get_mut(&key) { // -------------+ 'r | |
Some(value) => value, // | | |
None => { // | | |
map.insert(key, V::default()); // | | |
// ^~~~~~ ERROR // | | |
map.get_mut(&key).unwrap() // | | |
} // | | |
} // | | |
} // v | |
fn main() { | |
let mut map: HashMap<&str, i32> = | |
[("Norway", 100), | |
("Denmark", 50), | |
("Iceland", 10)] | |
.iter().cloned().collect(); | |
{ | |
let key = "Norway"; | |
let v = get_default(&mut map, key); // -+ 'r | |
// +-- get_default() -----------+ // | | |
// | match map.get_mut(&key) { | // | | |
// | Some(value) => value, | // | | |
// | None => { | // | | |
// | .. | // | | |
// | } | // | | |
// +----------------------------+ // | | |
println!("{}", v); // | | |
} // <--------------------------------------+ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
error[E0499]: cannot borrow `*map` as mutable more than once at a time | |
--> src/main.rs:10:13 | |
| | |
7 | match map.get_mut(&key) { | |
| --- first mutable borrow occurs here | |
... | |
10 | map.insert(key, V::default()); | |
| ^^^ second mutable borrow occurs here | |
... | |
14 | } | |
| - first borrow ends here | |
error[E0499]: cannot borrow `*map` as mutable more than once at a time | |
--> src/main.rs:11:13 | |
| | |
7 | match map.get_mut(&key) { | |
| --- first mutable borrow occurs here | |
... | |
11 | map.get_mut(&key).unwrap() | |
| ^^^ second mutable borrow occurs here | |
... | |
14 | } | |
| - first borrow ends here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
error[E0499]: cannot borrow `*map` as mutable more than once at a time | |
--> src/main.rs:10:13 | |
| | |
2 | fn get_default<'r,K,V:Default>(map: &'r mut HashMap<K,V>, | |
| -- lifetime `'r` defined here | |
... | |
7 | match map.get_mut(&key) { | |
| - --- first mutable borrow occurs here | |
| _____| | |
| | | |
8 | | Some(value) => value, | |
9 | | None => { | |
10 | | map.insert(key, V::default()); | |
| | ^^^ second mutable borrow occurs here | |
11 | | map.get_mut(&key).unwrap() | |
12 | | } | |
13 | | } | |
| |_____- returning this value requires that `*map` is borrowed for `'r` | |
error[E0499]: cannot borrow `*map` as mutable more than once at a time | |
--> src/main.rs:11:13 | |
| | |
2 | fn get_default<'r,K,V:Default>(map: &'r mut HashMap<K,V>, | |
| -- lifetime `'r` defined here | |
... | |
7 | match map.get_mut(&key) { | |
| - --- first mutable borrow occurs here | |
| _____| | |
| | | |
8 | | Some(value) => value, | |
9 | | None => { | |
10 | | map.insert(key, V::default()); | |
11 | | map.get_mut(&key).unwrap() | |
| | ^^^ second mutable borrow occurs here | |
12 | | } | |
13 | | } | |
| |_____- returning this value requires that `*map` is borrowed for `'r` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
returning this value requires that `*map` is borrowed for `'r` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct List<T> { | |
value: T, | |
next: Option<Box<List<T>>>, | |
} | |
fn to_refs<T>(mut list: &mut List<T>) -> Vec<&mut T> { | |
let mut result = vec![]; | |
loop { | |
result.push(&mut list.value); | |
if let Some(mut n) = list.next.as_mut() { | |
list = &mut n; | |
} else { | |
return result; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
error[E0597]: `n` does not live long enough | |
--> src/main.rs:11:25 | |
| | |
11 | list = &mut n; | |
| ^ borrowed value does not live long enough | |
... | |
14 | } | |
| - borrowed value only lives until here | |
| | |
note: borrowed value must be valid for the anonymous lifetime #1 defined on the function body at 6:1... | |
--> src/main.rs:6:1 | |
| | |
6 | / fn to_refs<T>(mut list: &mut List<T>) -> Vec<&mut T> { | |
7 | | let mut result = vec![]; | |
8 | | loop { | |
9 | | result.push(&mut list.value); | |
... | | |
15 | | } | |
16 | | } | |
| |_^ | |
error[E0499]: cannot borrow `list.value` as mutable more than once at a time | |
--> src/main.rs:9:26 | |
| | |
9 | result.push(&mut list.value); | |
| ^^^^^^^^^^ mutable borrow starts here in previous iteration of loop | |
... | |
16 | } | |
| - mutable borrow ends here | |
error[E0499]: cannot borrow `list.next` as mutable more than once at a time | |
--> src/main.rs:10:30 | |
| | |
10 | if let Some(mut n) = list.next.as_mut() { | |
| ^^^^^^^^^ mutable borrow starts here in previous iteration of loop | |
... | |
16 | } | |
| - mutable borrow ends here | |
error[E0506]: cannot assign to `list` because it is borrowed | |
--> src/main.rs:11:13 | |
| | |
9 | result.push(&mut list.value); | |
| ---------- borrow of `list` occurs here | |
10 | if let Some(mut n) = list.next.as_mut() { | |
11 | list = &mut n; | |
| ^^^^^^^^^^^^^ assignment to borrowed `list` occurs here | |
warning: variable does not need to be mutable | |
--> src/main.rs:10:21 | |
| | |
10 | if let Some(mut n) = list.next.as_mut() { | |
| ----^ | |
| | | |
| help: remove this `mut` | |
| | |
= note: #[warn(unused_mut)] on by default |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment