Hi there,
I need some help here. What I am trying to do is to mapp some objects and modify them if a new value is presented. In the test case below i have a mock-up of what i am trying to achieve.
Let say i iterate through a vec of keys for each key i want to check if it exists in my hashmap and if it does not i want to create a new object X with a key value set and another value (vals[i]) pushed to it. On the other hand if it already exists i would like to just push the value (vals[i]).
use std::collections::HashMap;
#[derive(Debug)]
struct X {
r: String,
v: Vec<u8>,
}
impl X {
pub fn new() -> Self {
X {
r: "x".to_string(),
v: Vec::new(),
}
}
fn add(&mut self, x: u8) -> &mut Self {
self.v.push(x);
self
}
fn set(&mut self, x: &str) -> &mut Self {
self.r = x.to_string();
self
}
}
fn main() {
let vals = vec![5u8; 4];
let keys = vec!["X".to_string(), "X".to_string(), "Y".to_string(), "Y".to_string()];
let mut res: HashMap<String, &mut X> = HashMap::default();
let mut i = 0;
while i < vals.len() {
res.entry(keys[i].clone())
.and_modify(|x| {
x.add(vals[i] + 1u8);
})
.or_insert(X::new().set(&keys[i]).add(vals[i]));
i += 1;
}
println!("{:#?}", res)
}
if i execute it i get
Compiling playground v0.0.1 (/playground)
error[E0716]: temporary value dropped while borrowed
--> src/main.rs:37:24
|
33 | res.entry(keys[i].clone())
| --- borrow later used here
...
37 | .or_insert(X::new().set(&keys[i]).add(vals[i]));
| ^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary value which is freed while still in use
|
= note: consider using a `let` binding to create a longer lived value
For more information about this error, try `rustc --explain E0716`.
error: could not compile `playground` (bin "playground") due to 1 previous error
I understand what compiler is complaining about but i do not know how to fix it
Can someone maybe provide some help here ?
Thank you