Я использую библиотеку redux-rs
, чтобы перенести шаблон redux в Rust. Я продолжаю получать странную ошибку несоответствия типов, поскольку пытаюсь использовать действия, которые являются частью жирного перечисления. Со следующим кодом:
fn root_reducer<T>(state: &State, action: &Action<T>) -> State
where
T: Fn(State) -> State,
{
match action {
// Travel: Move the player forward by distance and move the date forward by days
Action::Travel(days, distance) => State {
date: state.date + *days,
miles: state.miles - distance,
..*state
},
// Rest: Regenerate one health (up to 5) by stopping for rest_days
Action::Rest(days) => State {
date: state.date + *days,
// No more than 5 health
health: if state.health < 5 {
state.health + 1
} else {
state.health
},
..*state
},
// Hunt: Add one hundred pounds of food by stopping for hunt_days
Action::Hunt => State {
date: state.date + Duration::days(state.hunt_days),
food: state.food + 100,
..*state
},
// Print the status of the game
Action::Status(mut status_function) => status_function(*state),
// Print commands and what they do
Action::Help(mut help_function) => help_function(*state),
// End the game
Action::Quit(mut quit_mock) => quit_mock(*state),
}
}
fn main() {
let inital_state = State {
date: Utc.ymd(2020, 3, 1),
miles: 2000,
food: 500,
health: 5,
hunt_days: 2,
};
let mut store = Store::new(root_reducer, inital_state);
}
Я получаю сообщение об ошибке:
type annotations needed for `redux_rs::store::Store<State, Action<T>>`
Я попытался добавить аннотацию типа <&dyn Fn(State) -> State>
к вызову Store::new
, но это только что закончилось до создания ошибки:
cannot move out of `action.0` which is behind a shared reference