I have a test in progress that does not compile:
#[test]
fn node_cost_dequeue() {
let mut queue: BinaryHeap<&NodeCost> = BinaryHeap::new();
let cost_1: NodeCost = NodeCost::new(1, 50, 0);
let cost_2: NodeCost = NodeCost::new(2, 30, 0);
let cost_3: NodeCost = NodeCost::new(3, 80, 0);
queue.push(&cost_1);
queue.push(&cost_2);
queue.push(&cost_3);
assert_eq!(2, (*queue.pop().unwrap()).id);
}
Results in error: cost_1 does not live long enough
With the additional info that "borrowed value dropped before borrower".
So I attempt to add explicit lifetime annotations.
#[test]
fn node_cost_dequeue() {
let mut queue: BinaryHeap<&'a NodeCost> = BinaryHeap::new();
let cost_1: NodeCost<'a> = NodeCost::new(1, 50, 0);
let cost_2: NodeCost<'a> = NodeCost::new(2, 30, 0);
let cost_3: NodeCost<'a> = NodeCost::new(3, 80, 0);
queue.push(&cost_1);
queue.push(&cost_2);
queue.push(&cost_3);
assert_eq!(2, (*queue.pop().unwrap()).id);
}
This results in use of undeclared lifetime name 'a
.
So, I attempt to declare it on the function
fn node_cost_dequeue<'a>() -> () {
But this results in error: functions used as tests must have signature fn() -> ()
Am I on the right track? How do I declare this lifetime?