pub fn vote(&mut self, ctx: &Context, candidate_num: u32) -> Result<()> {
        if !self.accepting_votes {
            return Err("Voting is closed.".to_string());
        }
        if candidate_num as usize >= self.candidates.len() {
            return Err(format!("Invalid candidate `{}`.", candidate_num));
        }
        if let Some(prev_vote) = self.voters.insert(ctx.sender(), candidate_num) {
            self.tally[prev_vote as usize] -= 1;
        }
        self.tally[candidate_num as usize] =
            self.tally[candidate_num as usize].checked_add(1).unwrap();
        Ok(())
    }
/// Cast a vote for a candidate.
/// `candidate_num` is the index of the chosen candidate in `Ballot::candidates`.
/// If you have already voted, this will change your vote to the new candidate.
/// Voting for an invalid candidate or after the ballot has closed will return an `Err`.

Cast a vote for a candidate.
`candidate_num` is the index of the chosen candidate in `Ballot::candidates`.
If you have already voted, this will change your vote to the new candidate.
Voting for an invalid candidate or after the ballot has closed will return an `Err`.
Cast a vote for a candidate . ` candidate_num ` is the index of the chosen candidate in ` Ballot :: candidates ` . If you have already voted this will change your vote to the new candidate . Voting for an invalid candidate or after the ballot has closed will return an ` Err ` .
pub fn vote(&mut self, ctx: &Context, candidate_num: u32) -> Result<()> {
        if !self.accepting_votes {
            return Err("Voting is closed.".to_string());
        }
        if candidate_num as usize >= self.candidates.len() {
            return Err(format!("Invalid candidate `{}`.", candidate_num));
        }
        if let Some(prev_vote) = self.voters.insert(ctx.sender(), candidate_num) {
            self.tally[prev_vote as usize] -= 1;
        }
        self.tally[candidate_num as usize] =
            self.tally[candidate_num as usize].checked_add(1).unwrap();
        Ok(())
    }
pub fn vote ( & mut self , ctx : & Context , candidate_num : u32 ) -> Result < ( ) > { if ! self . accepting_votes { return Err ( " " . to_string ( ) ) ; } if candidate_num as usize >= self . candidates . len ( ) { return Err ( format ! ( " " candidate_num ) ) ; } if let Some ( prev_vote ) = self . voters . insert ( ctx . sender ( ) , candidate_num ) { self . tally [ prev_vote as usize ] -= 1 ; } self . tally [ candidate_num as usize ] = self . tally [ candidate_num as usize ] . checked_add ( 1 ) . unwrap ( ) ; Ok ( ( ) ) }
Cast a vote for a candidate.
Cast a vote for a candidate .
&mut self
ctx
&Context
candidate_num
u32