#referencing an internal struct field at instantiation (?)

7 messages · Page 1 of 1 (latest)

rigid copper
#

I have my struct which looks like this:

struct Example<'s> {
   current_state: &'s State
   state_table: Box<[State]>
}```

and a function to generate this struct that takes in a state table and references the first element as its current state.

```rs
impl Example<'_> {
  fn new(state_table: Box<[State]>) {
      Example { 
        current_state: &state_table[0],
        state_table
      }
   }
}```

Obviously, this code does not compile because I am referencing `table` which will be dropped. Is there any way I can instead reference the `Example`'s own `state_table`?
radiant lintel
#

use an index

rigid copper
#

this is sad.

radiant lintel
limber apex
#

It is possible to construct the above manually with pointers and quite a bit of unsafe

covert oar