How to properly create a member Vec
? What am I missing here?
struct PG {
names: &mut Vec<String>,
}
impl PG {
fn new() -> PG {
PG { names: Vec::new() }
}
fn push(&self, s: String) {
self.names.push(s);
}
}
fn main() {
let pg = PG::new();
pg.push("John".to_string());
}
If I compile this code, I get:
error[E0106]: missing lifetime specifier
--> src/main.rs:2:12
|
2 | names: &mut Vec<String>,
| ^ expected lifetime parameter
If I change the type of names
to &'static mut Vec<String>
, I get:
error[E0308]: mismatched types
--> src/main.rs:7:21
|
7 | PG { names: Vec::new() }
| ^^^^^^^^^^
| |
| expected mutable reference, found struct `std::vec::Vec`
| help: consider mutably borrowing here: `&mut Vec::new()`
|
= note: expected type `&'static mut std::vec::Vec<std::string::String>`
found type `std::vec::Vec<_>`
I know I can use parameterized lifetimes, but for some other reason I have to use static
.