A "is never read" warning I do not understand [answered, thank you!]


#1

Hi,
Im just at the beginning of learing rust, and I stumbled while doing “hello world” programs over the following I don’t understand:

This “program” here:

fn main() {
    let points: i32 = 10;
    let mut saved_points: u32 = 0;
    saved_points = points as u32;
    println!("{}", saved_points);
}

gives me the following warning while building:

src\main.rs:3:9: 3:25 warning: value assigned to `saved_points` is never read, #[warn(unused_assignments)] on by default
src\main.rs:3     let mut saved_points: u32 = 0;
                      ^~~~~~~~~~~~~~~~

I thought that I read saved_points while using the println! macro, or not?

Thank you very much for any input about this warning.

Gregor


#2

The initial value 0 is never read… Initialize saved_points directly.

let mut saved_points = points as u32;

#3

Thank you for pointing me at this.
Witchcraft! I mean very clever compiler, stupid me.


#4

Heh, everyone I talk to seems to agree that rustc is really darned helpful about that sort of thing.


#5

You also don’t have to have a placeholder value or initialize it immediately. let mut saved_points: u32 works as well. It even fails to compile when you try to read from it and it was not initialized.