Rust Mutable vec in a Struct
Hello!
I'm trying to learn rust but I cant seem to wrap my head around this problem, and why i'm getting this error.|
28 | fn update\_cell(&mut self, cell: usize, value: &str) {
| --------- - let's call the lifetime of this reference \'1\\
| |
| has type \&mut Board<'2>\\
29 | self.squares\[cell\] = value;
| \^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^\^ assignment requires that \'1\\ must outlive \'2\\
Below is my program, i removed some functions that works fine.
struct Board<'a> {
squares: Vec<&'a str>,
}
impl Board<'_> {
fn update_cell(&mut self, cell: usize, value: &str) {
self.squares[cell] = value;
}
}
fn main() {
let mut board = Board {
squares: vec![" ", " ", " ", " ", " ", " ", " ", " ", " "],
};
board.update_cell(0, "X");
board.draw_board();
}
I'm trying real hard to get into rust and i know it's good to run into these things at compile time.
Is it because i already have a mutable reference to board in the main scope?
How would i then solve this issue?
Could some helpful soul please guide me?