Here's what to remember about arrays and slices: arrays are of fixed size and the size needs to be known at compile time. The type is [T; n], where T is the type of values in the array and n the size of the array:
- Slices are views into existing things and their size is more dynamic. The type is &[T]
- To pass sequences to functions, favor slices
- An Index trait can be used to make your own types indexable or sliceable
Here are some tasks you should try for yourself:
- Make a 10-element fixed array of numbers.
- Take a slice that contains all elements of the previous array except the first and the last.
- Use for x in xs (shown briefly in Chapter 1, Getting Your Feet Wet) to sum all the numbers in both the array and the slice. ...