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

Sequences

One more thing to cover and then we can wrap up the basics. Rust has a few built-in ways to construct sequences of data: arrays and tuples. Then, it has a way to take a view to a piece of that data: slices. Thirdly, it has several data structures as libraries, of which we will cover Vectors (for dynamically growable sequences) and HashMaps (for key/value data).

Arrays are C-like: they have a fixed length that you need to specify along with the type of the elements of the array when declaring it. The notation for array types is [<type>, <size>]:

// arrays.rs fn main() {   let numbers: [u8; 10] = [1, 2, 3, 4, 5, 7, 8, 9, 10, 11];   let floats = [0.1, 0.2, 0.3];   println!("The first number is {}", numbers[0]);  for number in &numbers ...

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