I'm encountering an issue with my Rust code. The compiler is throwing an error saying that new_position is possibly uninitialized when I try to use it in the println! statement.
Here's a minimal example of the code that reproduces the issue:
fn main() {
let from_position = 10;
let to_tab_position = 20;
let next_position: Option<i32> = None;
let prev_position: Option<i32> = None;
let mut new_position; // Declaration without initialization
for _ in 0..2 {
if from_position < to_tab_position {
new_position = if next_position.is_none() {
to_tab_position + 20
} else {
(to_tab_position + next_position.unwrap()) / 2
};
} else {
new_position = if prev_position.is_none() {
to_tab_position - 20
} else {
(((to_tab_position as f64) + (prev_position.unwrap() as f64)) / 2.0).ceil() as i32
};
}
println!("new_position: {}", new_position);
}
println!("Final new_position: {}", new_position);
}
The error message I’m getting is:
error[E0381]: used binding `new_position` is possibly-uninitialized
--> src/main.rs:27:40
|
7 | let mut new_position; // Declaration without initialization
| ---------------- binding declared here but left uninitialized
...
11 | / new_position = if next_position.is_none() {
12 | | to_tab_position + 20
13 | | } else {
14 | | (to_tab_position + next_position.unwrap()) / 2
15 | | };
| |_____________- binding initialized here in some conditions
16 | } else {
17 | / new_position = if prev_position.is_none() {
18 | | to_tab_position - 20
19 | | } else {
20 | | (((to_tab_position as f64) + (prev_position.unwrap() as f64)) / 2.0).ceil() as i32
21 | | };
| |_____________- binding initialized here in some conditions
...
27 | println!("Final new_position: {}", new_position);
| ^^^^^^^^^^^^ `new_position` used here but it is possibly-uninitialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0381`.
From what I understand, the compiler thinks that new_position might not always be initialized due to the conditional assignment in the loop, but I’m not sure how to fix this issue. Can someone explain why this happens and how I can make sure new_position is always initialized?
Thanks!
I tried running the code with a for loop, but I encountered a compiler error saying that new_position is possibly uninitialized. To debug, I removed the loop, and the code ran successfully without the error. I expected the program to compile and print the value of new_position without any errors, but when the loop was included, it resulted in an uninitialized variable error. I’m now trying to figure out why this happens with the loop and how to fix it.
This happens because the compiler doesn't attempt to prove that the loop body will execute at least once. While we can trivially see this, the compiler does not detect that, and it is true that if the loop were to run zero times, the variable would not be initialized.
The simplest solution is to just initialize the variable. While the compiler does not notice the loop body is guaranteed to execute at least once, the optimizer almost certainly will, and should elide the initialization.