0

So I have created a MarkovChain struct and accompanying State and Transition structs

pub struct MarkovChain {
    pub states: Vec<State>,
    pub transitions: Vec<Transition>,
}

pub struct State {
    pub name: String,
    pub transitions: Vec<Transition>,
}

pub struct Transition {
    pub name: String,
    pub probability: f64,
    pub next_state: &State,
}

The idea is we have a Markov Chain within which we have states (nodes) and Transitions (directed edges) and I shouldn't need to store the next node with each edge, only the pointer/reference to the node.

I have implemented a function to add transitions but it gives me an error.

pub fn add_transition(&mut self, name: String, probability: f64, next_state: &State) {
  self.transitions.push(Transition {
    name: name,
    probability: probability,
    next_state: next_state,
  });
}

The error is

> pub next_state: &State,
> expected named lifetime parameter
>    |
> help: consider introducing a named lifetime parameter
>    |
> 29 ~ pub struct Transition<'a> {
> 30 |     pub name: String,
> 31 |     pub probability: f64,
> 32 ~     pub next_state: &'a State,

This makes me suspect I've done something silly or unidiomatic here and have sort of coded myself into a corner. I'm trying to pick up rust again and am still getting to grips with the styles/idioms. Can anybody suggest a better approach?

FJ_OC
  • 231
  • 2
  • 7

0 Answers0