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

}