Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change months array to const for better clarity #309

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down