Declare first

It’s possible to declare variable bindings first, and initialize them later. However, this form is seldom used, as it may lead to the use of uninitialized variables.

1. fn main() {
2. // Declare a variable binding
3. let a_binding;

5. {
6. let x = 2;

8. // Initialize the binding
9. a_binding = x * x;
10. }

12. println!("a binding: {}", a_binding);

14. let another_binding;

16. // Error! Use of uninitialized binding
17. println!("another binding: {}", another_binding);
18. // FIXME ^ Comment out this line

20. another_binding = 1;

22. println!("another binding: {}", another_binding);
23. }

The compiler forbids use of uninitialized variables, as this would lead to undefined behavior.