| Constant Naming Convention |
|
the naming convention for Constants are similar to that of variables all characters in a constant name are usually in uppercase the let keyword is not used to declare a constant syntax const VARIABLE_NAME:dataType = value;usage fn main() {
const USER_LIMIT:i32 = 100; // Declare a integer constant
const PI:f32 = 3.14; //Declare a float constant
println!("user limit is {}",USER_LIMIT); //Display value of the constant
println!("pi value is {}",PI); //Display value of the constant
}
|
| Constants vs Variables |
|
| Shadowing of Variables and Constants |
|
Rust allows programmers to declare variables with the same name the new variable overrides the previous variable essentially changes the value of an immutable to a new immutable with a different value fn main() {
let salary = 100.00;
let salary = 1.50 ;
// reads first salary
println!("The value of salary is :{}",salary);
}
above code declares two variables by the name salarythe second variable shadows or hides the first variable while displaying output |