Code
fn main() {
let v1: Vec<i32> = vec![1, 2, 3];
let mut iter = v1.iter();
let mut first_encounter = true;
let mut token: i32;
let mut following_token: i32;
loop {
if first_encounter {
if let Some(token) = iter.next() {
match iter.next() {
Some(temp) => {
following_token = *temp;
first_encounter = false;
println!("{} followed by {}", token, following_token);
}
_ => {
println!("{} is last", token);
}
}
} else {
break;
}
} else {
token = following_token;
first_encounter = true;
match iter.next() {
Some(temp) => {
following_token = *temp;
first_encounter = false;
println!("{} followed by {}", token, following_token);
}
_ => {
println!("{} is last", token);
}
}
}
}
}
Current output
Compiling playground v0.0.1 (/playground)
error[E0381]: used binding `following_token` isn't initialized
--> src/main.rs:25:21
|
7 | let mut following_token: i32;
| ------------------- binding declared here but left uninitialized
...
17 | _ => {
| - if this pattern is matched, `following_token` is not initialized
...
21 | } else {
| ------ if the `if` condition is `false` and this `else` arm is executed, `following_token` is not initialized
...
25 | token = following_token;
| ^^^^^^^^^^^^^^^ `following_token` used here but it isn't initialized
|
help: consider assigning a value
|
7 | let mut following_token: i32 = 42;
| ++++
For more information about this error, try `rustc --explain E0381`.
error: could not compile `playground` (bin "playground") due to 1 previous error
Desired output
The code compiles and running the binary produces:
1 followed by 2
2 followed by 3
3 is last
Rationale and extra context
As the usage flagged by rustc can only be reached when first_encounter is false, following_token cannot be uninitialized as it has been set at the point first_encounter is set to false in the other branch. It cannot be reached in the first loop iteration.
My actual code is way more complicated where I iterate over complex structs where I cannot define a meaningful default value.
Other cases
Rust Version
rustc 1.95.0 (59807616e 2026-04-14)
binary: rustc
commit-hash: 59807616e1fa2540724bfbac14d7976d7e4a3860
commit-date: 2026-04-14
host: x86_64-unknown-linux-gnu
release: 1.95.0
LLVM version: 22.1.2
Anything else?
Is code is also available on the Rust Playground.
This may be related to #121733.
Code
Current output
Desired output
Rationale and extra context
As the usage flagged by
rustccan only be reached whenfirst_encounterisfalse,following_tokencannot be uninitialized as it has been set at the pointfirst_encounteris set tofalsein the other branch. It cannot be reached in the first loop iteration.My actual code is way more complicated where I iterate over complex structs where I cannot define a meaningful default value.
Other cases
Rust Version
Anything else?
Is code is also available on the Rust Playground.
This may be related to #121733.