Variable Bindings

Rust provides type safety via static typing. Variable bindings can be type annotated when declared. However, in most cases, the compiler will be able to infer the type of the variable from the context, heavily reducing the annotation burden.

Values (like literals) can be bound to variables, using the let binding.

1. fn main() {
2. let an_integer = 1u32;
3. let a_boolean = true;
4. let unit = ();

6. // copy `an_integer` into `copied_integer`
7. let copied_integer = an_integer;

9. println!("An integer: {:?}", copied_integer);
10. println!("A boolean: {:?}", a_boolean);
11. println!("Meet the unit value: {:?}", unit);

13. // The compiler warns about unused variable bindings; these warnings can
14. // be silenced by prefixing the variable name with an underscore
15. let _unused_variable = 3u32;

17. let noisy_unused_variable = 2u32;
18. // FIXME ^ Prefix with an underscore to suppress the warning
19. }