Rustlings: Live!

Collections and things

What is a collection?

Collections are dynamic data structures. The amount of data does not need to be known at compile time and can grow or shrink as the program runs.

 

Under the hood, it uses a global dynamic memory allocator (AKA the heap)

Types of Collections

  • Vec<T>
    
  • String
  • HashMap<K, V>
    

Vectors

Vectors allow you to store more than one value in a single data structure that puts all the values next to each other in memory.

 

Vectors can only store values of the same type.

Vectors vs Arrays vs Slices

// An array
let arr = [0, 1, 2, 3, 4];

// A slice of a: 1, 2, and 3
let middle = &arr[1..4]; 

 

// A vector
let v = vec![1, 2, 3];
v.push(6);

Indexing in vectors

Indexing in vectors

Indexing in vectors (modified)

Iteration in vectors

Strings

Strings are growable, mutable, owned, UTF-8 encoded string type.

 

String && &str (string slice)

Indexing in Strings

Text

Iteration in Strings

Made with Slides.com