先声明
It is possible to declare variable bindings first and initialize them later, but all variable bindings must be initialized before they are used: the compiler forbids use of uninitialized variable bindings, as it would lead to undefined behavior.
It is not common to declare a variable binding and initialize it later in the function. It is more difficult for a reader to find the initialization when initialization is separated from declaration. It is common to declare and initialize a variable binding near where the variable will be used.
fn main() { // 声明一个变量绑定 let a_binding; { let x = 2; // 初始化绑定 a_binding = x * x; } println!("绑定:{}", a_binding); let another_binding; // 错误!使用未初始化的绑定 println!("另一个绑定:{}", another_binding); // 修复:^ 注释掉此行 another_binding = 1; println!("另一个绑定:{}", another_binding); }