Skip to content

Instantly share code, notes, and snippets.

@JuanCaicedo
Last active October 13, 2018 00:21
Show Gist options
  • Save JuanCaicedo/fa33ec618c68560e4c39a15d2cfd03a0 to your computer and use it in GitHub Desktop.
Save JuanCaicedo/fa33ec618c68560e4c39a15d2cfd03a0 to your computer and use it in GitHub Desktop.
struct Action {
_type: String,
amount: u32
}
fn update_counter(state:u32, action: Action) {
let Action {_type, amount} = action;
return match _type.as_ref() {
"INCREMENT" => state + amount,
"DECREMENT" => state - amount,
"RESET" => 0,
_ => state
}
}
fn main() {
let newState = update_counter(4, Action {
_type: "INCREMENT",
amount: 2
});
println!("{}", newState); // 6
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment