Variables

95 阅读1分钟

Variables

let name = "Michael";
let age = 32;

Rust is a strongly typed language

Variable type is optional if it can be inferred

Type can be specified explicitly

let amount: i64 = 842323424324234234;
let amount = 842323424324234234;    // error

Names can contain letters, numbers and underscores

Must start with a letter or underscore

Follow snake_case naming convention

Immutable by default

let length = 34;
length = 35;    // error

Can be declared mutable

let mut length = 34;
length = 35;

Shadowing is allowed

let color = "blue";
let color = "red";
println!("Color is {}.", color);    // Color is red.

Declaring multiple variables simultaneously

let (a, b, c) = (2, 3, 4);