I'm getting an error that "xo" and the string literal in the vector of vectors have different lifetimes. I was able to find a workaround by converting the literals to Strings
via to_string()
but I still would like to understand this error.
fn main() {
let mut tic_tac = vec![
vec!["[ ]", "[ ]", "[ ]"],
vec!["[ ]", "[ ]", "[ ]"],
vec!["[ ]", "[ ]", "[ ]"],
];
let letter = "[x]";
make_move(&letter, 1, &mut tic_tac);
make_move(&letter, 4, &mut tic_tac);
}
fn make_move(xo: &str, position: i32, board: &mut Vec<Vec<&str>>) {
if position < 4 && position <= 1 {
match position {
1 => board[0][0] = xo,
2 => board[0][1] = xo,
3 => board[0][2] = xo,
_ => (),
}
}
}
error[E0623]: lifetime mismatch
--> src/main.rs:18:32
|
15 | fn make_move(xo: &str, position: i32, board: &mut Vec<Vec<&str>>) {
| ---- ----
| |
| these two types are declared with different lifetimes...
...
18 | 1 => board[0][0] = xo,
| ^^ ...but data from `xo` flows into `board` here