Borrowed value does not live long enough in BTreeSet test
⚓ Rust 📅 2026-02-07 👤 surdeus 👁️ 1This program is derived from tests for BTreeSet:
use pstd::collections::BTreeSet;
fn main() {
let s246 = BTreeSet::from([2, 4, 6]);
let s23456 = BTreeSet::from_iter(2..=6);
let mut iter = s246.difference(&s23456);
assert_eq!(iter.size_hint(), (0, Some(3)));
assert_eq!(iter.next(), None);
let s12345 = BTreeSet::from_iter(1..=5);
iter = s246.difference(&s12345);
assert_eq!(iter.size_hint(), (0, Some(3)));
assert_eq!(iter.next(), Some(&6));
assert_eq!(iter.size_hint(), (0, Some(0)));
assert_eq!(iter.next(), None);
}
It doesn't compile, I get an error:
error[E0597]: `s12345` does not live long enough
--> src/main.rs:11:28
|
10 | let s12345 = BTreeSet::from_iter(1..=5);
| ------ binding `s12345` declared here
11 | iter = s246.difference(&s12345);
| ^^^^^^^ borrowed value does not live long enough
...
16 | }
| -
| |
| `s12345` dropped here while still borrowed
| borrow might be used here, when `iter` is dropped and runs the destructor for type `pstd::collections::btree_set::Difference<'_, i32, CustomTuning>`
|
= note: values in a scope are dropped in the opposite order they are defined
For more information about this error, try `rustc --explain E0597`.
I vaguely understand the error, and if I change the line
iter = s246.difference(&s12345);
to
let mut iter = s246.difference(&s12345);
the error goes away and everything is fine. What I don't understand though is that if I use std rather than pstd ( the crate I am developing ) the error also goes away. Is there some magic to suppress the error in std? What is going on?
1 post - 1 participant
🏷️ Rust_feed