Hello there, Newbie rustacian here!
Going through some refactoring I found no simpler way to express the match expression bellow. Would anybody have a suggestion to make my code look prettier?
Always accept &[T] over &Vec<T>. The former is strictly superior than the latter.
The&self.voters[voter] will panic if the voter is already in the hashmap. To avoid panic, use following pattern and handles all three cases explicitly.
Hello KrishnaSannasi, thanks for the answer!
This approach looks the clearest for me by far. Although, I'd like to add a voter's vote only if he haven't voted yet. Isn't there a way to take Some(voter) only if self.voters.get_mut(voter) points to Some(None)?
That's what the code does. Option::get_or_insert only fills in the Option if it is in the None state. So if the voter already voted this is s no op, and if they have not voted, this fills in the Option