diff --git a/Common Programming Concepts/Tuples and Arrays/The Array Type/src/main.rs b/Common Programming Concepts/Tuples and Arrays/The Array Type/src/main.rs index 1b7a72c1..0bed6364 100644 --- a/Common Programming Concepts/Tuples and Arrays/The Array Type/src/main.rs +++ b/Common Programming Concepts/Tuples and Arrays/The Array Type/src/main.rs @@ -4,10 +4,10 @@ fn main() { let first = a[0]; let second = a[1]; - let months = ["January", "February", "March", + const MONTHS: [&str; 12] = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; - println!("The last month is {}", months[months.len() - 1]); + println!("The last month is {}", MONTHS[MONTHS.len() - 1]); } diff --git a/Common Programming Concepts/Tuples and Arrays/The Array Type/task.md b/Common Programming Concepts/Tuples and Arrays/The Array Type/task.md index 02fb61e4..a5a55e5a 100644 --- a/Common Programming Concepts/Tuples and Arrays/The Array Type/task.md +++ b/Common Programming Concepts/Tuples and Arrays/The Array Type/task.md @@ -12,14 +12,14 @@ fn main() { Arrays are useful when you want your data allocated on the stack rather than the heap (we will discuss the stack and the heap more in Chapter 4) or when you want to ensure you always have a fixed number of elements. An array isn’t as flexible as the vector type, though. A vector is a similar collection type provided by the standard library that is allowed to grow or shrink in size. If you’re unsure whether to use an array or a vector, you should probably use a vector. Chapter 8 discusses vectors in more detail. -An example of when you might want to use an array rather than a vector is in a program that needs to know the names of the months of the year. It’s very unlikely that such a program will need to add or remove months, so you can use an array because you know it will always contain 12 elements: +An example of when you might want to use an array rather than a vector is in a program that needs to know the names of the months of the year. It’s very unlikely that such a program will need to add or remove months, so you can use an array because you know it will always contain 12 elements. Since we don't even need to change the names of the months, we can use the `const` keyword: ```rust -let months = ["January", "February", "March", "April", "May", "June", "July", - "August", "September", "October", "November", "December"]; +const MONTHS: [&str; 12] = ["January", "February", "March", "April", "May", "June", + "July", "August", "September", "October", "November", "December"]; ``` -You would write an array’s type by using square brackets, and within the brackets include the type of each element, a semicolon, and then the number of elements in the array, like so: +You would write an array’s type by using square brackets, and within the brackets include the type of each element, a semicolon, and then the number of elements in the array, like we did with `MONTHS` or like this array: ```rust let a: [i32; 5] = [1, 2, 3, 4, 5];