|
| 1 | +# Introduction |
| 2 | + |
| 3 | +An `array` is a collection of objects of the same type `T`, stored in contiguous |
| 4 | +memory. |
| 5 | +Arrays are created using brackets `[]`, and their length, which is known |
| 6 | +at compile time, is part of their type signature `[T; length]`(...) |
| 7 | + |
| 8 | +Unlike arrays in some other languages, arrays in Rust have a fixed length. |
| 9 | + |
| 10 | + |
| 11 | +## Arrays |
| 12 | +### Defining an array |
| 13 | +We write the values in an array as a comma-separated list inside square |
| 14 | +brackets: |
| 15 | + |
| 16 | +```rust |
| 17 | +let my_array = [1, 2, 3, 4, 5]; |
| 18 | +``` |
| 19 | + |
| 20 | +We write an array’s type using square brackets with the type of each element, a |
| 21 | +semicolon, and then the number of elements in the array, like so: |
| 22 | + |
| 23 | +```rust |
| 24 | +let my_array: [i32; 5] = [1, 2, 3, 4, 5]; |
| 25 | +``` |
| 26 | + |
| 27 | +Here, `i32` is the type of each element. After the semicolon, the number 5 |
| 28 | +indicates the array contains five elements. |
| 29 | + |
| 30 | +You can also initialize an array to contain the same value for each element by |
| 31 | +specifying the initial value, followed by a semicolon, and then the length of |
| 32 | +the array in square brackets, as shown here: |
| 33 | + |
| 34 | +```rust |
| 35 | +let my_array = [3; 5]; |
| 36 | +// => [3, 3, 3, 3, 3] |
| 37 | +``` |
| 38 | + |
| 39 | +### Accessing Array Elements |
| 40 | +You can access elements of an array using indexing, like this: |
| 41 | + |
| 42 | +```rust |
| 43 | +let a = [1, 2, 3, 4, 5]; |
| 44 | + |
| 45 | +let first = a[0]; |
| 46 | +let second = a[1]; |
| 47 | +``` |
| 48 | + |
| 49 | + |
| 50 | +### Invalid Array Element Access |
| 51 | +Let’s see what happens if you try to access an element of an array that is past |
| 52 | +the end of the array. |
| 53 | + |
| 54 | +```rust |
| 55 | +let a = [1, 2, 3, 4, 5]; |
| 56 | + |
| 57 | +let sixth = a[5]; |
| 58 | +``` |
| 59 | + |
| 60 | +The above program will fail to compile with an error similar to: |
| 61 | +```txt |
| 62 | +let value = a[5]; |
| 63 | + ^^^^^^^^^^^^ index out of bounds: the length is 5 but the index is 5 |
| 64 | +``` |
| 65 | + |
| 66 | +Now consider when you don't know the index you want to access and it is provided |
| 67 | +to you via a function parameter or a user input: |
| 68 | + |
| 69 | +```rust |
| 70 | +fn access_element(index: usize) -> i32 { |
| 71 | + let a = [1, 2, 3, 4, 5]; |
| 72 | + return a[index]; |
| 73 | +} |
| 74 | + |
| 75 | +fn main() { |
| 76 | + access_element(5); |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +In this case, the Rust compiler cannot know if the index is out of bounds. |
| 81 | +The code will compile without errors but fail at runtime with the following error. |
| 82 | + |
| 83 | +```txt |
| 84 | +thread 'main' panicked at 'index out of bounds: the len is 5 but the index is 5' |
| 85 | +``` |
| 86 | + |
| 87 | + |
| 88 | +### Use as a type |
| 89 | + |
| 90 | +An array type can be used as a variable type, function parameter or return type. |
| 91 | + |
| 92 | +```rust |
| 93 | +let my_var: [i32; 5]; |
| 94 | + |
| 95 | +fn my_func(input: [i32; 5]) -> [i32; 5] {} |
| 96 | +``` |
| 97 | + |
| 98 | +### Changing array elements |
| 99 | +Like for any other variable, arrays also have to be defined using `mut` keyword |
| 100 | +to allow change. |
| 101 | +An element can be changed by using assignment operator while accessing it via an index. |
| 102 | + |
| 103 | +```rust |
| 104 | +let mut my_array = [2, 2, 3]; |
| 105 | +my_array[0] = 1; |
| 106 | +// => [1, 2, 3] |
| 107 | +``` |
| 108 | + |
| 109 | +In the above example, we access the first element via it's index 0 and assign a |
| 110 | +new value to it. |
| 111 | + |
| 112 | +## For loops |
| 113 | + |
| 114 | +It’s often useful to execute a block of code more than once. |
| 115 | +For this task, Rust provides several loops, which will run through the code |
| 116 | +inside the loop body to the end and then start immediately back at the |
| 117 | +beginning. |
| 118 | + |
| 119 | +### `for` and range |
| 120 | + |
| 121 | +The `for in` construct can be used to iterate through an `Iterator`. |
| 122 | +We will learn more about `Iterators` in later concepts. |
| 123 | +One of the easiest ways to create an iterator is to use the range notation `a..b`. |
| 124 | +This yields values from a (inclusive) to b (exclusive) in steps of one. |
| 125 | + |
| 126 | +```rust |
| 127 | +// A for loop that iterates 10 times |
| 128 | +for n in 0..10 { |
| 129 | + // n will have a starting value of 0 |
| 130 | + // n will have the last value as 9 |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +Alternatively, `a..=b` can be used for a range that is inclusive on both ends. The above can be written as: |
| 135 | + |
| 136 | +```rust |
| 137 | +// A for loop that iterates 10 times |
| 138 | +for n in 1..=10 { |
| 139 | + // n will have a starting value of 1 |
| 140 | + // n will have the last value as 10 |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +### Break and Continue a loop |
| 145 | +A for loop can be halted by using the `break` statement. |
| 146 | + |
| 147 | +```rust |
| 148 | +// A for loop that iterates 10 times |
| 149 | +for n in 1..=10 { |
| 150 | + println!(n); |
| 151 | + break; |
| 152 | +} |
| 153 | +// => 1 |
| 154 | +``` |
| 155 | + |
| 156 | +One can conditionally break a for loop as follows: |
| 157 | + |
| 158 | +```rust |
| 159 | +// A for loop that iterates 10 times |
| 160 | +for n in 1..=10 { |
| 161 | + println!(n); |
| 162 | + if n == 5 { |
| 163 | + break; |
| 164 | + } |
| 165 | +} |
| 166 | +// => 1 |
| 167 | +// => 2 |
| 168 | +// => 3 |
| 169 | +// => 4 |
| 170 | +// => 5 |
| 171 | +``` |
| 172 | + |
| 173 | +A `continue` statement can be used to skip over an iteration. |
| 174 | +```rust |
| 175 | +// A for loop that iterates 10 times |
| 176 | +for n in 1..=10 { |
| 177 | + println!(n); |
| 178 | + continue; |
| 179 | +} |
| 180 | +// => 1 |
| 181 | +``` |
| 182 | + |
| 183 | +Similar to `break`, one can also conditionally use `continue`: |
| 184 | + |
| 185 | +```rust |
| 186 | +// A for loop that iterates 10 times |
| 187 | +for n in 1..=10 { |
| 188 | + println!(n); |
| 189 | + if n >= 5 { |
| 190 | + continue; |
| 191 | + } |
| 192 | +} |
| 193 | +// => 1 |
| 194 | +// => 2 |
| 195 | +// => 3 |
| 196 | +// => 4 |
| 197 | +// => 5 |
| 198 | +``` |
| 199 | + |
| 200 | +### Looping Through an Array with `for` |
| 201 | +The following shows an example of looping through an array using the `for in` construct. |
| 202 | + |
| 203 | +```rust |
| 204 | +let a = [10, 20, 30, 40, 50]; |
| 205 | + |
| 206 | +for element in a { |
| 207 | + println!("the value is: {element}"); |
| 208 | +} |
| 209 | + |
| 210 | +// => the value is: 10 |
| 211 | +// => the value is: 20 |
| 212 | +// => the value is: 30 |
| 213 | +// => the value is: 40 |
| 214 | +// => the value is: 50 |
| 215 | + |
| 216 | +``` |
0 commit comments