#Unused variable

3 messages · Page 1 of 1 (latest)

lapis veldt
#

So I have this code and for some reason the compiler complains that my input is unused? Is it possible to rewrite it somehow or am I missing something?

    fn place_piece(&mut self) {
        let mut input = 0;

        loop {
            input = get_input();
            if input > 8 {
                println!("The number must be between 1 and 9");
                continue;
            }

            if self.taken_slots[input] != 0 {
                println!("There's already a piece at {} \nTry Again...", input);
            } else {
                break;
            }
        }

        match self.turn {
            Turn::Player1 => {
                self.taken_slots[input] = 1;
                self.p1_value *= PIECE_VALUE[input];
                self.turn = Turn::Player2
            }
            Turn::Player2 => {
                self.taken_slots[input] = 2;
                self.p2_value *= PIECE_VALUE[input];
                self.turn = Turn::Player1
            }
        }

        println!("Placed a piece at: {}", input + 1);
    }

Oh and my get_input function just returns a usize number

maiden laurel
#

If you do let mut input; it'll fix it. Or more idiomatically, you can assign it by breaking the loop with a value:

let input = loop {
  let input = get_input();
  ...
  break input;
};
spark hill
#

Just so you know, i'm pretty sure it wants you to write

let mut input;

Seaishs solution is nicer tho imo