Hi folks,
i am quite new to rust
I have successfully created a HashMap in the fn main ():
let fk_list: HashMap <&str,&str> =
[("0101","Text1"),
("0201","Text2"),
.
. here come many more elements
.
("0203","Text3")].iter.cloned.collect();
and can access it e.g.:
let description = fk_list.get("9999").unwrap_or(&"No value");
As the HashMap is quite long and because I want to create some methods which do the unwrapping, I want to take it off the main fn and integrate it in a struct.
struct Fieldtab<'a>{
fk_list: HashMap<&'a str,&'a str>,
}
impl Fieldtab {
fn read() -> Fieldtab {
let fk_list: HashMap <&str,&str> =
[("0101","Text1"),
("0201","Text2"),
.
.
.
("0203","Text3")].iter().cloned().collect();
}
}
fn main should contain something like that:
let list = Fieldtab::read("version1");
as there will be several versions of the HashMap in the future read() should get a paramter "version1", "version2"... but this is not important for now.
let description = list.fk_list.get("8410").unwrap_or("No value");
}
Unfortunately I am quite new to rust so I am still struggling from time to time with borrowing and lifetimes'
but perhaps there ist a complete other and better way to do what I want to do..
Any ideas?
Thanks
cremofix