Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Зв’язування змінних

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
    // Please note that warnings may not be shown in a browser
}