位置:首页 > > Rust变量绑定-声明

Rust变量绑定-声明

可以先声明变量绑定,以后(然后)在初始化。 然而,这种形式很少使用,因为它可能导致使用未初始化的变量。
fn main() {
    // Declare a variable binding
    let a_binding;

    {
        let x = 2;

        // Initialize the binding
        a_binding = x * x;
    }

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

    let another_binding;

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

    another_binding = 1;

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