I am trying insert pairs of a variable into a HashSet
, after checking if the pair is present already. I then need to do downstream work with the pair, within the same context. Here is a playground reproducing my problem:
use std::collections::HashSet;
fn main() {
let mut h = HashSet::new();
let a = 1;
let b = 2;
if h.contains(&(&a, &b)) {
println!("fail");
}
h.insert(&(&a, &b));
}
error[E0597]: borrowed value does not live long enough
--> src/main.rs:10:15
|
10 | h.insert(&(&a, &b));
| ^^^^^^^^ - temporary value dropped here while still borrowed
| |
| temporary value does not live long enough
11 | }
| - temporary value needs to live until here
|
= note: consider using a `let` binding to increase its lifetime
error[E0597]: `a` does not live long enough
--> src/main.rs:10:17
|
10 | h.insert(&(&a, &b));
| ^ borrowed value does not live long enough
11 | }
| - `a` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created
error[E0597]: `b` does not live long enough
--> src/main.rs:10:21
|
10 | h.insert(&(&a, &b));
| ^ borrowed value does not live long enough
11 | }
| - `b` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created
How can I check if a
and b
are in the set, then insert them afterwards if they are not, and then do other things with them? How are they being borrowed if I am loading in a reference to them within the same scope?