In Rust, variables are declared using the let
keyword followed by the variable name and optional type annotation. Here's how you declare variables in Rust:
let x: i32 = 10;
let y = 3.14;
Rust distinguishes between mutable and immutable variables. By default, variables are immutable. You can make a variable mutable by using the mut
keyword. Here's an example:
let mut counter = 0;
counter += 1; // This is allowed because counter is mutable
Rust has several primitive data types, including integers, floating-point numbers, booleans, and characters.
- Integer: Represents whole numbers and can be signed or unsigned. Example:
i32
,u64
. - Floating-Point: Represents numbers with a fractional part. Example:
f32
,f64
. - Boolean: Represents true or false values. Example:
bool
. - Character: Represents a single Unicode character. Example:
char
.
Rust also provides compound data types like tuples and arrays.
- Tuple: A fixed-size collection of heterogeneous elements. Example:
(1, "hello", true)
. - Array: A fixed-size collection of homogeneous elements. Example:
[1, 2, 3, 4, 5]
.
A tuple is a fixed-size collection of heterogeneous elements. Each element in the tuple can have a different type. Tuples are useful when you want to group together multiple values of different types.
let person: (i32, &str, bool) = (25, "John", true);
Accessing elements of a tuple can be done using indexing:
let age = person.0; // Access the first element (age)
let name = person.1; // Access the second element (name)
let is_adult = person.2; // Access the third element (is_adult)
An array is a fixed-size collection of homogeneous elements. All elements in an array must have the same type. Arrays are useful when you need a collection of items that have a consistent type and size.
let numbers: [i32; 5] = [1, 2, 3, 4, 5];
Accessing elements of an array can also be done using indexing:
let first_number = numbers[0]; // Access the first element
let second_number = numbers[1]; // Access the second element
Rust supports type inference, allowing the compiler to deduce the types of variables based on their usage. This reduces the need for explicit type annotations.
let x = 42; // Rust infers that x has type i32
Constants are similar to variables, but their values cannot be changed once set. They are declared using the const
keyword and must be annotated with a type.
const PI: f64 = 3.14;
- Prefer immutable variables by default and only use mutable variables when necessary to minimize unexpected changes in your code.
- Use descriptive variable names to make your code self-explanatory and easier to understand.
- When choosing between different data types, consider the range of values you need to represent and the precision required.
Imagine you're developing a weather application in Rust. You use immutable variables to store temperature readings and mutable variables to track changes in weather conditions throughout the day.
Understanding variables and data types is fundamental to writing Rust programs. By mastering these concepts, you'll be well-equipped to tackle more complex programming tasks with confidence.