The compiler is wrong saying that a variable "used here but it is possibly-uninitialized". Even the wording "possible" here indicates the compiler is taking too much responsibility. Probability is 0 in my case:
enum Importance
{
Very,
Notvery,
}
fn main()
{
let test = Importance::Very;
let mut a: u32;
let mut importance_evaluated: bool = false;
match test
{
Importance::Very =>
{
a = 99;
importance_evaluated = true;
}
Importance::Notvery => (),
}
if importance_evaluated == false
{
return
}
println!("{}", a);
}
The code does not compile because of the error:
error[E0381]: used binding `a` is possibly-uninitialized
--> main.rs:31:20
|
12 | let mut a: u32;
| ----- binding declared here but left uninitialized
...
22 | Importance::Notvery => (),
| ------------------- if this pattern is matched, `a` is not initialized
...
31 | println!("{}", a);
| ^ `a` used here but it is possibly-uninitialized
|
But I'm sure 100% that uninitialized variable will never reach the println, and the compiler is wrong.
Is there a way to force compiler to ignore this line?
One way is to just initialize a to 0.
The second way is to make a an Option as @quinedot suggests. This will also eliminate the need for a separate importance_evaluated variable.
A third way is to use MaybeUninit.