0

I have these 2 pieces of code:

fn main() {
    let mut x: int = 5;
    x = 6;
    println!("value {}", x);
}  

With this code, the compiler will raise warning:

the value is never read at let x: int = 5

But with the following code, the compiler doesn't.

struct Point {
    x: int,
    y: int,
}
fn main() {
    let mut p = Point {x: 1i, y: 2i};
    p.x = 5;
    println!("value {}, {}", p.x, p.y);
}

Why does that happens? We never read the value when x = 1i. Rather, we read the value at x = 5i. So why the compiler doesn't raise warning like the code before?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
user3071121
  • 605
  • 2
  • 8
  • 12

1 Answers1

2

I think this happens because the compiler only analyzes immediate local variables and does not go deeper into structures. I imagine that full analysis would require a rather complex algorithm, and there is little need for it.

Or maybe it is a bug in the compiler, or, more likely, an unimplemented feature. You can submit a ticket to the issue tracker if you think it is important.

Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296