Probably this question already has been asked, maybe with different wording, but I couldn't find it. If this gets closed due to this, apologies, but I did try before.
In fact, the problem is simple:
use std::collections::BTreeSet;
pub struct Service {
blobs: BTreeSet<u16>,
}
impl Service {
fn new() -> Self {
let bt: BTreeSet<u16> = BTreeSet::new();
Self { blobs: bt }
}
fn insert(&self, val: u16) -> bool {
&self.blobs.insert(val);
true
}
fn print(&self) {
println!("{}", self.blobs.len());
}
}
fn main() {
let s: Service = Service::new();
s.print();
s.insert(4);
s.print();
}
When compiling this, I get:
`self` is a `&` reference, so the data it refers to cannot be borrowed as mutable
What I understand here, is that actually, I can't just update the treeset, as it's immutable.
So I tried with
pub struct Service {
blobs: &'static mut BTreeSet<u16>,
}
and
fn new() -> Self {
let bt: &mut BTreeSet<u16> = &mut BTreeSet::new();
Self { blobs: bt }
}
but that yields
creates a temporary value which is freed while still in use
(while the original error is still there, so now I have two...)
I understand I should somehow make the BTreeSet mutable, but how?
&mut selfininsert()?