Module lessons (1/2)
Vec<T>, String UTF-8 and HashMap
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
Stringvs&strand whys[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
- 11.
Vec<T>: Resizable contiguous lists - 22.
Stringvs&str: whys[0]doesn't compile - 33.
HashMap<K, V>and.entry().or_insert() - 4Practical exercises
- 5Check for understanding
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.