This code work
#[test]
fn should_get_a_next_transition() {
//let mut node2 = Node::new("node2".to_string());
let mut node3 = Node::new("node3".to_string());
let mut node1 = Node::new("node1".to_string());
//node1.add_next(&mut node2, 85);
node1.add_next(&mut node3, 12);
// call
let transition = node1.get_next_mut("node3");
// assertions
let tr = transition.unwrap();
assert_eq!("node3", tr.node.get_id());
assert_eq!(12, tr.weight);
}
But this one does not work
#[test]
fn should_get_a_next_transition() {
let mut node2 = Node::new("node2".to_string());
let mut node3 = Node::new("node3".to_string());
let mut node1 = Node::new("node1".to_string());
node1.add_next(&mut node2, 85);
node1.add_next(&mut node3, 12);
// call
let transition = node1.get_next_mut("node3");
// assertions
let tr = transition.unwrap();
assert_eq!("node3", tr.node.get_id());
assert_eq!(12, tr.weight);
}
Error message :
error[E0597]: `node3` does not live long enough ] 0/3: shortest-way, shortest-way
--> src\graph.rs:225:29
|
225 | node1.add_next(&mut node3, 12);
| ^^^^^ borrowed value does not live long enough
...
234 | }
| - `node3` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created
warning: variable does not need to be mutable
--> src\graph.rs:221:13
|
221 | let mut node3 = Node::new("node3".to_string());
| ----^^^^^
| |
| help: remove this `mut`
|
= note: #[warn(unused_mut)] on by default
error: aborting due to previous error
please advise.
Here is a playaground to that gather all the code that have the issue