1

Sorry if the question isn't phrased very well. The explanation and code will hopefully be better.

The following code is a strip down version of a game where a bunch of cells are drawn to the screen and once you select one it does something.

#[derive(Debug)]
struct Cell {
    x: usize,
}

struct World<'a> {
    cells: Vec<Cell>,
    selected_cell: Option<&'a mut Cell>,
}

impl<'a> World<'a> {
    fn print_cells(&self) {
        for cell in &self.cells {
            println!("{:?}", cell);
        }
    }
}

fn main() {
    let mut w = World {
        cells: Vec::with_capacity(20),
        selected_cell: None,
    };

    for i in 0..w.cells.capacity() {
        let c = Cell {
            x: i,
        };

        w.cells.push(c);
    }

    main_loop(&mut w);
}

fn main_loop(w: &mut World) {
    loop {
        w.print_cells();

        if true {
            w.selected_cell = w.cells.get_mut(5);
        }

        match w.selected_cell {
            Some(mut cell) => {
                cell.x = 45;
                println!("selected {:?}", cell);
            },
            None => {},
        }

        if true {
            break;
        }
    }
}

The problem here is that I'm struggling with the lifetimes of the objects and The mutable/immutable references.

This gives me the error explicit lifetime required in the type of w.

explicit lifetime required in the type of <code>w</code>

The sublime text plugin gives some tips and I tried some suggestions but none work.

fn main_loop<'a>(mut w: &'a World) { // gives me: explicit lifetime required in the type of `w`
fn main_loop<'a>(mut w: &'a World<'a>) {

The one above works but it gives several errors

enter image description here

At this point I'm just shooting in the dark, adding and removing &, mut, 'a and <> on everything to see if works.

Playground

Lucas Steffen
  • 1,244
  • 2
  • 10
  • 22
  • 2
    Does this answer your question? [Why can't I store a value and a reference to that value in the same struct?](https://stackoverflow.com/questions/32300132/why-cant-i-store-a-value-and-a-reference-to-that-value-in-the-same-struct) – loganfsmyth Jun 09 '20 at 23:45
  • Have you considered storing the index of the selected value? – loganfsmyth Jun 09 '20 at 23:46
  • @loganfsmyth makes me think what I'm trying to achieve is impossible, since the compiler can't guarantee that selected_cell will hold a valid reference. Also that I understand even less about lifetimes. – Lucas Steffen Jun 10 '20 at 01:11
  • @loganfsmyth keeping the index is what I did in the original code - just to avoid this situation really; But I was wondering if it's really possible or I'm just going against what rust teaches. – Lucas Steffen Jun 10 '20 at 01:13
  • 1
    Just a thought experiment: What do you expect to happen if - in `main_loop` after `w.selected_cell = w.cells.get_mut(5)` - you say `w.cells.clear()`? What should happen to `w.selected_cell`? – phimuemue Jun 10 '20 at 08:06
  • @phimuemue that's why I think I'm fighting the compiler. I know it won't happen, but rust doesn't. – Lucas Steffen Jun 10 '20 at 15:58
  • @LucasSteffen If you really insist on having this architecture, you might get away with storing the index into the vector. – phimuemue Jun 10 '20 at 16:00

0 Answers0