Hi, I'm really new at Rust's world and I was wondering how push a vector without taking the ownership of the vector inside a callback
let mut users: Vec<User> = vec![];
submit.set_callback(move |_| {
if name_input.value().len() > 0 || age_input.value().len() > 0 {
println!("{}, {}", name_input.value(), age_input.value());
let user = User {name: name_input.value().to_string(), age: to_u8(age_input.value())};
name_input.set_value("");
age_input.set_value("");
//i wanna do this but not taking the ownership here
users.push(user)
}
})
error[E0597]: `users` does not live long enough
--> src/main.rs:30:21
|
30 | let mut_users = &mut users;
| ^^^^^^^^^^ borrowed value does not live long enough
31 | / submit.set_callback(move |_| {
32 | | if name_input.value().len() > 0 || age_input.value().len() > 0 {
33 | | println!("{}, {}", name_input.value(), age_input.value());
34 | | let user = User {name: name_input.value().to_string(), age: to_int_u(age_input.value())};
... |
42 | | }
43 | | });
| |______- argument requires that `users` is borrowed for `'static`
...
51 | }
| - `users` dropped here while still borrowed
Yeah, I wondered if there might be a 'static involved with the callback. That basically means "the type (the closure in this case) cannot contain any borrows (non-'static lifetimes)".