We can use any names as variable names, however, there are some rules we should follow:
-
Rust is a case sensitive language. Hence, lowercase variables and uppercase variables are different. For example:
age
is different fromAGE
name
is different fromName
-
Variables must start with either a letter or an underscore. For example,
let age = 31; // valid and good practice
let _age = 31; // valid variable
let 1age = 31; // inavlid variable
- Variable names can only contain letters, digits and an underscore character. For example,
let age1 = 31; // valid variable
let age_num = 31; // valid variable
let s@lary = 52352; // invalid variable
- Use underscore if we need to use two words as variable names. For example,
let first name = "Jack"; // invalid variable
let first_name = "Jack"; // valid variable
let first-name = "Jack"; // invalid variable
Note:
Always try to give meaningful names to your variables. For example,
name
,age
,number
are better names thann
,ag
,nm
.