|
| 1 | +### 4. Variables and Data Types |
| 2 | + |
| 3 | +#### Declaring Variables in Rust |
| 4 | + |
| 5 | +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: |
| 6 | + |
| 7 | +```rust |
| 8 | +let x: i32 = 10; |
| 9 | +let y = 3.14; |
| 10 | +``` |
| 11 | + |
| 12 | +#### Mutable vs Immutable Variables |
| 13 | + |
| 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: |
| 15 | + |
| 16 | +```rust |
| 17 | +let mut counter = 0; |
| 18 | +counter += 1; // This is allowed because counter is mutable |
| 19 | +``` |
| 20 | + |
| 21 | +#### Primitive Data Types |
| 22 | + |
| 23 | +Rust has several primitive data types, including integers, floating-point numbers, booleans, and characters. |
| 24 | + |
| 25 | +- **Integer:** Represents whole numbers and can be signed or unsigned. Example: `i32`, `u64`. |
| 26 | +- **Floating-Point:** Represents numbers with a fractional part. Example: `f32`, `f64`. |
| 27 | +- **Boolean:** Represents true or false values. Example: `bool`. |
| 28 | +- **Character:** Represents a single Unicode character. Example: `char`. |
| 29 | + |
| 30 | +#### Compound Data Types: Tuple and Array |
| 31 | + |
| 32 | +Rust also provides compound data types like tuples and arrays. |
| 33 | + |
| 34 | +- **Tuple:** A fixed-size collection of heterogeneous elements. Example: `(1, "hello", true)`. |
| 35 | +- **Array:** A fixed-size collection of homogeneous elements. Example: `[1, 2, 3, 4, 5]`. |
| 36 | + |
| 37 | +##### Tuple |
| 38 | + |
| 39 | +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. |
| 40 | + |
| 41 | +```rust |
| 42 | +let person: (i32, &str, bool) = (25, "John", true); |
| 43 | +``` |
| 44 | + |
| 45 | +Accessing elements of a tuple can be done using indexing: |
| 46 | + |
| 47 | +```rust |
| 48 | +let age = person.0; // Access the first element (age) |
| 49 | +let name = person.1; // Access the second element (name) |
| 50 | +let is_adult = person.2; // Access the third element (is_adult) |
| 51 | +``` |
| 52 | + |
| 53 | +##### Array |
| 54 | + |
| 55 | +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. |
| 56 | + |
| 57 | +```rust |
| 58 | +let numbers: [i32; 5] = [1, 2, 3, 4, 5]; |
| 59 | +``` |
| 60 | + |
| 61 | +Accessing elements of an array can also be done using indexing: |
| 62 | + |
| 63 | +```rust |
| 64 | +let first_number = numbers[0]; // Access the first element |
| 65 | +let second_number = numbers[1]; // Access the second element |
| 66 | +``` |
| 67 | + |
| 68 | +#### Type Inference in Rust |
| 69 | + |
| 70 | +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. |
| 71 | + |
| 72 | +```rust |
| 73 | +let x = 42; // Rust infers that x has type i32 |
| 74 | +``` |
| 75 | + |
| 76 | +#### Constants vs Variables |
| 77 | + |
| 78 | +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. |
| 79 | + |
| 80 | +```rust |
| 81 | +const PI: f64 = 3.14; |
| 82 | +``` |
| 83 | + |
| 84 | +#### Best Practices |
| 85 | + |
| 86 | +- Prefer immutable variables by default and only use mutable variables when necessary to minimize unexpected changes in your code. |
| 87 | +- Use descriptive variable names to make your code self-explanatory and easier to understand. |
| 88 | +- When choosing between different data types, consider the range of values you need to represent and the precision required. |
| 89 | + |
| 90 | +#### Real-World Example |
| 91 | + |
| 92 | +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. |
| 93 | + |
| 94 | +#### Conclusion |
| 95 | + |
| 96 | +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. |
0 commit comments