Building CS50's Plurality in Rust is Kicking my ass

Hello! I am trying to build the CS50 "Plurality" problem from problem set 4 and have run into some trouble.

The problem asks you to accept a command line argument like

cargo run Alice Bob Charlie

It then asks you to create two functions, Vote and print_winner.

I am struggling with taking the command line arguments and storing them in a struct called Candidate.

My code looks like this

use std::env;


pub struct Candidate {
    name: String, 
    vote: usize,
}



fn main() {
    let args: Vec<String> = env::args().skip(1).collect();
    let candidates: Vec<Candidate> = Vec::new();

// Does not work
    for name in args.iter_mut() {
        candidates.name = name;
        candidates.vote = 0; 
    }
}

The for loop does not work and I am unsure the best way to construct my Vec<Candidate> so that It is able to store the names provided in the command line and votes = 0;

I would really appreciate some suggestions!

Posted Jun 11, 2026, 2:47 AM UTC

25
Loading comments…