I am working on the following code all day, ( here is the playpen)
pub struct Rule<S, T> {
pub state: S,
pub symbol: Option<T>,
pub next_state: S
}
impl<S: PartialEq, T: PartialEq> Rule<S, T> {
pub fn apply_to(&self, state: &S, symbol: &Option<T>) -> bool {
self.state == *state && self.symbol == *symbol
}
}
pub struct NFATransitions<S, T> {
pub rules: HashSet<Rule<S, T>>
}
impl<S: Eq + Hash + Clone, T: Eq + Hash> NFATransitions<S, T> {
pub fn next_states(&self, states: &HashSet<S>, symbol: &Option<T>) -> HashSet<S> {
states.iter().flat_map(|state| {
self.next_states_for(state, symbol).iter().map(|s| s.clone())
}).collect()
}
fn next_states_for(&self, state: &S, symbol: &Option<T>) -> HashSet<S> {
self.rules.iter().filter_map(|rule| {
if rule.apply_to(state, symbol) { Some(rule.next_state.clone()) } else { None }
}).collect()
}
}
Code is just a hash shell used for nfa transition rules. (This is not what concerns me)
flat_map- this is where I got the compilation error. It seems strange to me, since commented out lines, which I think have the same behavior as well flat_map, can succeed.
I can not understand how the error occurs error: borrowed value does not live long enough.
Any ideas?
source
share