| Rules for Naming a Variable |
|
variables in Rust are associated with a specific data type the data type determines
|
| Syntax |
|
the data type is optional while declaring a variable the data type is inferred from the value assigned to the variable fn main() {
let fees = 25_000;
let salary:f64 = 35_000.00;
println!("fees is {} and salary is {}",fees,salary);
}
|
| Immutable |
|
by default variables are immutable − read only the variable's value cannot be changed once a value is bound to a variable name fn main() {
let fees = 25_000;
println!("fees is {} ",fees);
fees = 35_000;
println!("fees changed is {}",fees);
}
compiler will raise an error due to re-assignment of immutable variable
|
| Mutable |
prefix the variable name with mut keyword to make it mutable
let mut variable_name = value;
let mut variable_name:dataType = value;
Let us understand this with an example
fn main() {
let mut fees:i32 = 25_000;
println!("fees is {} ",fees);
fees = 35_000;
println!("fees changed is {}",fees);
}
|