I'm having trouble with the follwoing code...
use std::collections::BTreeSet;
use maybe_owned::MaybeOwned;
struct Thing<'a, T> {
set: BTreeSet<MaybeOwned<'a, T>>
}
impl<'a, T: Ord> Thing<'a, T> {
fn take(&mut self, x: T){
let y = self.set.take(&MaybeOwned::Borrowed(&x));
}
}
gives the compiler error
error[E0597]: `x` does not live long enough
--> src/main.rs:10:53
|
8 | impl<'a, T: Ord> Thing<'a, T> {
| -- lifetime `'a` defined here
9 | fn take(&mut self, x: T){
10 | let y = self.set.take(&MaybeOwned::Borrowed(&x));
| ------------------------------------^^--
| | |
| | borrowed value does not live long enough
| argument requires that `x` is borrowed for `'a`
11 | }
| - `x` dropped here while still borrowed
However x is clearly not borrowed at that point because the MaybeOwned has fallen out of scope and therefore the enclosed borrow has fallen out of scope.
How can I tell the rust compiler that this is fine?