I have a Simple structure with a HashSet with a lifetime:
Please let me know how can I correct lines 3 & 4:
pub struct MyStruct<'a> {
set: HashSet<&'a str>,
}
impl<'a> MyStruct<'a> {
fn fill(&mut self) {
let b = "b";
let c = String::from("c");
let d = format!("{}", "d");
self.set.insert("a"); // 1- works
self.set.insert(b); // 2- works
//self.set.insert(&c); // 3- Not works
//self.set.insert(&d); // 4- Not works
}
}
fn main() {
let mut foo = MyStruct{set:HashSet::new()};
{
let e = String::from("e");
let f = format!("{}", "f");
foo.set.insert(&e); // 5- works (compare with 3)
foo.set.insert(&f); // 6- works (compare with 4)
}
let mut bar = MyStruct{set:HashSet::new()};
bar.fill();
}
Hello,
Add println!("{:?}", foo); to line 29 and 5, 6 will stop working. Take ownership of the objects instead of references otherwise the objects have to live as long as MyStruct.
It works because 1, 2 have static lifetime. You can do: let b: &'static str = "b";.
I would say it depends of the context. For this case, it wouldn't work.
Something similar in Attempting to implement a Tree using Rust - #2 by RustyYato.
The string literals worked only because they're a special case where a string view is into a "leaked" memory, so its scope is the whole program.
You must remove <'a> from the struct and change temporary string view &str to an owned string String (both store the string by reference, but one is temporary and limited to a scope, the other is permanent and can be used after }).
String is just as immutable as &'static str unless you declare it mut. For most HashMaps, I would use String for keys, to let the map control ownership.
No. You do not declare lifetimes. You can only annotate lifetimes to express relationships between them. You cannot change an object's lifetime with annotations.