Title here
Summary here
In Rust, variables are explicitly declared and used by the compiler to check type-correctness of function calls.
fn main() {
// let declares 1 or more variables.
let a = "initial";
println!("{}", a);
// You can declare multiple variables at once.
let (b, c): (i32, i32) = (1, 2);
println!("{} {}", b, c);
// Rust will infer the type of initialized variables.
let d = true;
println!("{}", d);
// Variables declared without a corresponding
// initialization are not allowed in Rust.
// Instead, we need to specify a type and initialize it:
let e: i32 = 0;
println!("{}", e);
// In Rust, we use let for both declaration and initialization.
// Variables are immutable by default.
let f = "apple";
println!("{}", f);
}
To run the program:
$ rustc variables.rs
$ ./variables
initial
1 2
true
0
apple
In Rust:
let mut
.:=
syntax, as let
is used for both declaration and initialization.Option
type.These differences reflect Rust’s focus on safety and preventing common programming errors at compile time.