Consider the following code:
#[derive(Clone, Copy, Debug)]
struct TestStruct
{
a: u8,
}
fn main()
{
let v = vec![TestStruct { a: 5 }];
let mut last_option = None;
for current_val in v
{
if let Some(last) = last_option
{
println!("{:?}", last.a);
}
last_option = Some(current_val);
}
}
This compiles, or at least it should. In reality I get the following error message:
error[E0282]: type annotations needed for `Option<T>`
--> src/main.rs:17:30
|
11 | let mut last_option = None;
| --------------- consider giving `last_option` the explicit type `Option<T>`, with the type parameters specified
...
17 | println!("{:?}", last.a);
| ^^^^ cannot infer type
|
= note: type must be known at this point
For more information about this error, try `rustc --explain E0282`.
Rust should – I think – be able to figure it out. Even stranger is that if I remove the .a
in last.a
on line 17
it suddenly works as expected.
What am I missing?
(
Rust version:
rustc 1.57.0-nightly (0eabf25b9 2021-10-06)
binary: rustc
commit-hash: 0eabf25b90396dead0b2a1aaa275af18a1ae6008
commit-date: 2021-10-06
host: x86_64-unknown-linux-gnu
release: 1.57.0-nightly
LLVM version: 13.0.0
)