I would like to have this kind of struct to save memory allocation:
pub struct MyStruct<'a> {
corpus: HashSet<String>,
names: HashMap<&'a String, u8>,
// we may need some other properties such as addresses: HashMap<&'a String, u8> ...etc from corpus
}
impl MyStruct<'_> {
pub fn new() -> MyStruct<'static> {
let mut corpus = HashSet::new();
corpus.insert("test".to_string());
corpus.insert("test2".to_string());
// insert more to the corpus...
let mut names = HashMap::default();
names.insert(corpus.get("test").unwrap(), 1);
MyStruct {
corpus,
names,
}
}
}
I want to save memory usage of the names
property by reusing the corpus
that is created from the new
method as in the example above. (The corpus and names might have about a few million names). Is it possible? or I must use names: HashMap<String, u8>
.
This is the error of course:
102 | names.insert(corpus.get("test").unwrap(), 1);
| ------------------ `corpus` is borrowed here
103 | / MyStruct {
104 | | corpus,
105 | | names,
106 | | }
| |_________^ returns a value referencing data owned by the current function