Lesson 3: Variables and Data Types in Rust
1. What is a Variable?
A variable is a named storage location used to store data in a program.
For example, we can store a name, age, price or quantity inside a variable.
Example
Output:
Explanation:
letis used to declare a variable.nameis the variable name."Hemant"is the stored value.ageis another variable.32is its stored value.
2. The let Keyword
In Rust, the let keyword is used to declare a variable.
Syntax
Example
3. Immutable Variables
By default, Rust variables are immutable.
Immutable means their values cannot be changed after initialization.
Example
Important: Rust makes variables immutable by default to prevent accidental changes to data.
4. Mutable Variables
If you want to change a variable's value, use the mut keyword.
Syntax
Example
Output:
Immutable vs Mutable
Immutable | Mutable |
|---|---|
Cannot change its value | Can change its value |
Uses | Uses |
Default behaviour | Requires explicit permission |
Example: | Example: |
5. Constants
A constant is a value that cannot be changed throughout the program.
Rust uses the const keyword to declare constants.
Syntax
Example
Output:
Rules of Constants
- Constants must have an explicit data type.
- Constants cannot be declared using
mut. - Constants must be initialized when declared.
- Constant values must be known at compile time.
Variable vs Constant
Variable | Constant |
|---|---|
Declared using | Declared using |
Can be mutable using | Always immutable |
Type can be inferred | Type must be specified |
Can be initialized at runtime | Value must be a compile-time constant |
6. Shadowing
Shadowing means declaring a new variable with the same name as an existing variable.
Rust allows this using let.
Example
Output:
The second declaration creates a new variable named age, which shadows the first one.
Shadowing with Different Data Types
Rust also allows shadowing with a different data type.
Output:
Here:
- First
numberis a string. - Second
numberis an integer.
Shadowing vs Mutability
Shadowing | Mutability |
|---|---|
Uses | Uses |
Creates a new variable | Changes the existing variable |
Can change data type | Cannot change the variable's declared type |
Example: | Example: |
7. Data Types in Rust
Rust is a statically typed programming language.
This means every variable has a data type known at compile time.
Rust has two major categories of data types:
- Scalar Types
- Compound Types
A. Scalar Data Types
Scalar types represent a single value.
Rust has four primary scalar types:
- Integer
- Floating-point
- Boolean
- Character
8. Integer Data Types
Integer types represent whole numbers without decimal points.
Examples:
Rust provides signed and unsigned integer types.
Signed Integer Types
Signed integers can store both positive and negative values.
Type | Size |
|---|---|
i8 | 8 bits |
i16 | 16 bits |
i32 | 32 bits |
i64 | 64 bits |
i128 | 128 bits |
isize | Architecture dependent |
Unsigned Integer Types
Unsigned integers can store zero and positive values only.
Type | Size |
|---|---|
u8 | 8 bits |
u16 | 16 bits |
u32 | 32 bits |
u64 | 64 bits |
u128 | 128 bits |
usize | Architecture dependent |
Example
Output:
9. Floating-Point Data Types
Floating-point types represent numbers with decimal points.
Rust provides two floating-point types:
Type | Size | Precision |
|---|---|---|
f32 | 32 bits | Single precision |
f64 | 64 bits | Double precision |
Rust uses f64 as the default floating-point type.
Example
Output:
10. Boolean Data Type
Boolean represents only two values:
truefalse
Its data type is bool.
Example
Output:
11. Character Data Type
The char type represents a single Unicode scalar value.
It uses single quotation marks.
Example
Output:
Important: A Rust char represents one Unicode scalar value, not necessarily one byte.
12. Type Inference
Rust can automatically determine a variable's data type from its assigned value.
This is called type inference.
Example
Rust automatically infers:
Variable | Inferred Type |
|---|---|
age | i32 |
price | f64 |
is_active | bool |
These are the default inferred types in this example.
13. Type Annotations
You can explicitly specify a variable's data type.
Example
Type Inference vs Type Annotation
Type Inference | Type Annotation |
|---|---|
Rust determines the type | Programmer specifies the type |
Less code | More explicit |
|
|
14. Type Conversion and Casting
Sometimes we need to convert one data type into another.
Rust supports explicit numeric casting using the as keyword.
Example: Integer to Float
Output:
Example: Float to Integer
Output:
Important: Converting a floating-point number to an integer using as discards the fractional part.
15. Important Summary
Concept | Syntax | Purpose |
|---|---|---|
Variable |
| Declare a variable |
Mutable Variable |
| Declare a changeable variable |
Constant |
| Declare a constant |
Shadowing |
| Create a new variable with the same name |
Integer |
| Store whole numbers |
Float |
| Store decimal numbers |
Boolean |
| Store true or false |
Character |
| Store one Unicode scalar value |
Casting |
| Convert numeric types |
16. Practice Exercises
Exercise 1: Basic Variables
Create variables for:
- Your name
- Your age
- Your city
- Your company name
Print all values.
Exercise 2: Mutable Variables
Create a mutable variable named salary.
- Assign 10000.
- Print the salary.
- Change it to 15000.
- Print the updated salary.
Exercise 3: Constants
Create a constant named GST_RATE with a value of 18.0.
Print the GST rate.
Exercise 4: Data Types
Create the following variables with explicit data types:
- age: i32
- price: f64
- quantity: u32
- is_available: bool
- grade: char
Print all values.
Exercise 5: Shadowing
Create a variable named number with the string value "500".
Then shadow it with an integer value of 500.
Print the final value.
Exercise 6: Type Casting
Create an integer variable with value 25.
Convert it into a floating-point value and print both variables.