Skip to main content
Module 7 · Lesson 1 of 213/24 in the course~18 min
Module lessons (1/2)

Vec<T>, String UTF-8 and HashMap

Lesson preview5 hands-on exercises readyCourse Rust · Module 7 — Collections and Error Handling

Manipulate dynamic vectors, understand why String doesn't index with [i], and update HashMap with .entry().or_insert().

What you'll learn in this lesson

  • Manage contiguous dynamic collections with Vec<T> (push, pop, get, safe iteration).
  • Understand the UTF-8 representation of String vs &str and why s[0] is not allowed.
  • Update HashMap<K, V> dictionaries idiomatically with the Entry API (.entry(k).or_insert(v)).

Overview

Unlike the fixed-length [T; N] arrays on the Stack, the collections in std::collections and Vec/String allocate their elements dynamically on the Heap, automatically growing or shrinking.

Create a vector with Vec::new() or with the macro vec![1, 2, 3]. To read without risking panic outside the index, always prefer .get(idx) which returns Option<&T>:

Sections covered in this lesson

  1. 11. Vec<T>: Resizable contiguous lists
  2. 22. String vs &str: why s[0] doesn't compile
  3. 33. HashMap<K, V> and .entry().or_insert()
  4. 4Practical exercises
  5. 5Check for understanding
Pro Lesson · Module 7 — Collections and Error Handling

Unlock "Vec<T>, String UTF-8 and HashMap" and the full Course Rust curriculum

Manipulate dynamic vectors, understand why `String` doesn't index with `[i]`, and update `HashMap` with `.entry().or_insert()`.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Course Rust plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.