I am trying to do something like this:
use std::io;
fn main() {
let is_male : bool;
let mut answer = String::new();
println!("Are you a male? (Input Y or N)");
io::stdin()
.read_line(&mut answer)
.expect("Failed to read line");
if answer == "Y" || answer == "y" {
is_male = true;
}
else if answer == "N" || answer == "n" {
is_male = false;
}
else {
println!("Invalid input!");
}
if is_male {
println!("You are a male.");
}
else {
println!("You are not a male.");
}
}
The error I got is:
error[E0381]: use of possibly-uninitialized variable: is_male
--> src\main.rs:22:9
|
22 | if !is_male {
| ^^^^^^^ use of possibly-uninitialized is_male
Does Rust not allow assigning boolean values this way? Or is there something wrong with the code?