- Rust - Discussion
- Rust - Useful Resources
- Rust - Quick Guide
- Rust - Concurrency
- Rust - Smart Pointers
- Rust - Iterator and Closure
- Rust - Package Manager
- Rust - File Input/ Output
- Rust - Input Output
- Rust - Generic Types
- Rust - Error Handling
- Rust - Collections
- Rust - Modules
- Rust - Enums
- Rust - Structure
- Rust - Slices
- Rust - Borrowing
- Rust - Ownership
- Rust - Array
- Rust - Tuple
- Rust - Functions
- Rust - Loop
- Rust - Decision Making
- Rust - Operators
- Rust - String
- Rust - Constant
- Rust - Variables
- Rust - Data Types
- Rust - HelloWorld Example
- Rust - Environment Setup
- Rust - Introduction
- Rust - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Rust - Variables
A variable is a named storage that programs can manipulate. Simply put, a variable helps programs to store values. Variables in Rust are associated with a specific data type. The data type determines the size and layout of the variable s memory, the range of values that can be stored within that memory and the set of operations that can be performed on the variable.
Rules for Naming a Variable
In this section, we will learn about the different rules for naming a variable.
The name of a variable can be composed of letters, digits, and the underscore character.
It must begin with either a letter or an underscore.
Upper and lowercase letters are distinct because Rust is case-sensitive.
Syntax
The data type is optional while declaring a variable in Rust. The data type is inferred from the value assigned to the variable.
The syntax for declaring a variable is given below.
let variable_name = value; // no type specified let variable_name:dataType = value; //type specified
Illustration
fn main() { let fees = 25_000; let salary:f64 = 35_000.00; println!("fees is {} and salary is {}",fees,salary); }
The output of the above code will be fees is 25000 and salary is 35000.
Immutable
By default, variables are immutable − read only in Rust. In other words, the variable s value cannot be changed once a value is bound to a variable name.
Let us understand this with an example.
fn main() { let fees = 25_000; println!("fees is {} ",fees); fees = 35_000; println!("fees changed is {}",fees); }
The output will be as shown below −
error[E0384]: re-assignment of immutable variable `fees` --> main.rs:6:3 | 3 | let fees = 25_000; | ---- first assignment to `fees` ... 6 | fees=35_000; | ^^^^^^^^^^^ re-assignment of immutable variable error: aborting due to previous error(s)
The error message indicates the cause of the error – you cannot assign values twice to immutable variable fees. This is one of the many ways Rust allows programmers to write code and takes advantage of the safety and easy concurrency.
Mutable
Variables are immutable by default. Prefix the variable name with mut keyword to make it mutable. The value of a mutable variable can be changed.
The syntax for declaring a mutable variable is as shown below −
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); }
The output of the snippet is given below −
fees is 25000 fees changed is 35000Advertisements