O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Takeaways and tasks

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:

  1. Make a 10-element fixed array of numbers.
  2. Take a slice that contains all elements of the previous array except the first and the last.
  3. 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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required