Rust变量绑定 - Rust教程

Rust通过静态类型提供类型安全。变量绑定可以在声明类型注解的时候。然而,在大多数情况下,编译器将能够推断出变量的类型从上下文,严重降低了注解负担。

值(如文本)可以绑定变量,使用 let 绑定。

fn main() {
    let an_integer = 1u32;
    let a_boolean = true;
    let unit = ();

    // copy `an_integer` into `copied_integer`
    let copied_integer = an_integer;

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

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

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