Overview
5 min read
In TypeScript you keep data in Array, string, Object/Map, and Set, and you reshape it with array methods like .map(), .filter(), and .reduce(). Rust ships the same shapes as concrete, typed standard-library collections — Vec<T>, String/&str, HashMap<K, V>, HashSet<T>, and the sorted BTreeMap/BTreeSet — and a far more powerful tool for processing them: the lazy iterator system. This section maps each TypeScript habit onto its idiomatic Rust collection, then shows how iterator adaptors and consumers replace your array-method muscle memory while doing strictly less work (nothing runs until you ask for a result).
What You’ll Learn
Section titled “What You’ll Learn”- How a JavaScript
Arraybecomes a typed, growableVec<T>—push/pop/get/indexing, iteration, thevec!macro, and explicit capacity so you can pre-allocate - Why Rust splits the one JavaScript
stringinto the owned, growableStringand the borrowed view&str, and what UTF-8 and byte-boundary slicing mean for “indexing” a string - The everyday
String/&strmethods —split,trim,replace,parse,chars()vsbytes()— and the idiomatic ways to build strings without re-allocating on every concatenation - How a plain object or
Mapbecomes aHashMap<K, V>whose keys and values are typed and owned, lookups returnOption<&V>instead ofundefined, and updates go through theentryAPI - How a JavaScript
Setbecomes aHashSet<T>with first-classunion/intersection/difference/symmetric_differenceset algebra - When to reach for the sorted collections
BTreeMap/BTreeSet— guaranteed key order andO(log n)range queries — instead of their hashing cousins - Why Rust’s iterator adaptors (
map,filter,take,skip,zip,enumerate) are lazy, doing no work until a consumer pulls values through them — the opposite of eager JavaScript array methods - The consuming adaptors that finish a chain —
collect,fold,sum,count,find,any/all,min/max,reduce— and how they line up withreduce/find/some/every - How to build your own lazy data producer by implementing the
Iteratortrait (onenextmethod) andIntoIteratorsoforloops work on your type - How to choose a collection deliberately using its documented Big-O cost model, pre-allocate with
with_capacity, and why an iterator chain compiles to essentially the same machine code as a hand-written loop
Topics
Section titled “Topics”| Topic | Description |
|---|---|
| Vectors | Array → Vec<T>: push/pop/get/indexing, iteration, capacity and growth, and the vec! macro. |
Strings: String vs &str | The critical split of one JS string into owned String vs borrowed &str: UTF-8, ownership, and slicing on byte boundaries. |
| String Manipulation | Common String/&str methods: split/trim/replace/parse, chars() vs bytes(), and building strings efficiently. |
| HashMaps | Object/Map → HashMap<K, V>: insert/get, the entry API, iteration, and ownership of keys and values. |
| Sets and HashSet | Set → HashSet<T>: membership tests and the union/intersection/difference set operations. |
| Sorted Collections: BTreeMap and BTreeSet | The sorted collections BTreeMap/BTreeSet: guaranteed ordering, range queries, and how they compare to HashMap. |
| Iterators | Array methods → iterator adaptors: lazy evaluation, and map/filter/take/skip/zip/enumerate. |
| Iterator Consumers | The consuming adaptors that run a chain: collect, fold, sum, count, find, any/all, min/max, reduce. |
| Custom Iterators | Implementing the Iterator trait, impl Iterator for your own type, and IntoIterator for for-loop support. |
| Collection Performance | Big-O characteristics, when to use Vec/HashMap/BTreeMap, capacity preallocation, and iterator vs loop. |
Learning Objectives
Section titled “Learning Objectives”By the end of this section, you will be able to:
- Choose the right container for a given job —
Vecfor ordered lists,String/&strfor text,HashMap/HashSetfor keyed lookup and uniqueness,BTreeMap/BTreeSetwhen order matters — and justify the choice with its cost model - Explain the
Stringvs&strdistinction, take a&strparameter for cheap borrowing, and slice strings safely on UTF-8 byte boundaries - Translate JavaScript array-method chains (
.map().filter().reduce()) into idiomatic Rust iterator pipelines, and explain why the Rust version is lazy and allocation-free until consumed - Use the
entryAPI to insert-or-update map values in a single lookup, and handle missing keys through theOptionthat lookups return instead of relying onundefined - Pick a consumer (
collect,fold,sum,find,any/all,max) to finish a chain, and recognize the compiler warning you get when an iterator is built but never consumed - Implement the
IteratorandIntoIteratortraits to make your own type loopable and inherit the entire adaptor toolbox - Pre-allocate with
with_capacity/reservewhere it pays off, and reason about when a manual loop and an iterator chain are equivalent
Prerequisites
Section titled “Prerequisites”- Section 05: Ownership — collections own their elements; pushing into a
Vecmoves a value in, indexing borrows, andinto_iter()consumes the collection. TheStringvs&strsplit is ownership applied to text, so be comfortable with moves, borrows, andClonefirst. - Section 06: Data Structures — collections hold your
structs andenums, lookups returnOption<T>, and you will pattern-match on the results. Knowing structs, enums, andOptionmakes this section click. - Section 02: Basics — concrete types (
usize,i64,f64), immutability-by-default andlet mut, and{:?}debug formatting all show up throughout.
Estimated Time
Section titled “Estimated Time”- Reading: 5-6 hours
- Hands-on Practice: 4-5 hours
- Exercises: 3 hours
- Total: 12-14 hours
Tip: Read the topics in order. Start with the containers —
Vec, then strings, then the maps and sets — so you know what shapes hold your data. Then learn the iterator system (iterators→iterator-consumers→custom-iterators), which is how you transform every one of those containers with the same vocabulary. Finish withcollection-performanceto tie the choices together. The single biggest mental shift for a TypeScript developer is laziness:arr.map(...)in JavaScript allocates a new array immediately, but a Rust iterator adaptor does nothing until a consumer runs it.
Next: Section 08: Error Handling → — Result, Option, the ? operator, and custom error types, replacing the throw/try/catch you used in TypeScript.