Collections
41 articles
How to Convert Between Collection Types in Rust
Convert Rust collection types by consuming the source with into_iter() and gathering elements into the target type using collect().
How to convert Vec to HashMap
Convert a Vec of tuples to a HashMap using into_iter() and collect().
How to filter a Vec
Filter a Rust Vec by chaining .into_iter().filter(|item| condition).collect() to create a new vector containing only matching elements.
How to Filter a Vector in Rust
Filter a Rust vector by chaining .filter() with a closure and .collect() to return a new vector of matching items.
How to Find an Element in a Vector in Rust
Find elements in a Rust vector safely using the get() method for indices or iter().find() for values.
How to find min max in Vec
You can find the minimum and maximum values in a `Vec` using the built-in `min()` and `max()` methods, which return `Option<&T>` for safe handling of empty collections, or by using the `minmax()` method available in Rust 1.57+ to compute both in a single pass.
How to flatten nested Vecs
Flatten nested Vecs in Rust using the iter().flatten().collect() method chain to merge inner vectors into a single flat vector.
How to Iterate Over a HashMap in Rust
Iterate over a Rust HashMap using a for loop with .iter() to access key-value pairs.
How to iterate over HashMap
Iterate over a Rust HashMap using a for loop with the iter() method to access key-value pairs.
How to merge two HashMaps
Merge two HashMaps in Rust by calling the extend method on the target map with the source map as an argument.
How to Merge Two HashMaps in Rust
Merge two Rust HashMaps by iterating over the second map and inserting its entries into the first using the Entry API.
How to Remove Duplicates from a Vector in Rust
You can remove duplicates from a `Vec` in Rust by either sorting and using the built-in `dedup` method for in-place modification, or by using a `HashSet` to preserve the original order while filtering unique elements.
How to remove duplicates from Vec
The most efficient way to remove duplicates from a `Vec` while preserving order is to sort the vector and call `dedup()`, which works in-place for types implementing `PartialEq` and `Ord`.
How to retain elements in Vec
Use the `retain` method with a closure to keep specific elements in a `Vec` and remove the rest in place.
How to sort a Vec
You can sort a `Vec` in place using the `.sort()` method for simple types or `.sort_by_key()` for custom ordering, while `.sorted()` from the `itertools` crate or `.into_iter().sorted()` provides a non-destructive alternative.
How to Sort a Vector in Rust
Sort a Rust vector in place using the sort or sort_by_key methods on a mutable collection.
How to split Vec into chunks
Use the chunks method to iterate over fixed-size groups or split_at_mut to divide a mutable vector into two distinct parts.
How to use BinaryHeap
Use BinaryHeap to create a priority queue that automatically sorts items by importance, allowing you to push tasks and pop the highest priority one instantly.
How to Use BinaryHeap (Priority Queue) in Rust
Use std::collections::BinaryHeap to efficiently manage a priority queue that automatically keeps the highest priority element accessible in constant time.
How to Use BTreeMap and BTreeSet in Rust
Use BTreeMap for sorted key-value pairs and BTreeSet for sorted unique values in Rust, requiring Ord trait implementation.
How to use BTreeMap vs HashMap
Use HashMap for speed and BTreeMap for sorted, deterministic key ordering in Rust.
How to Use drain and retain on Collections in Rust
Use drain to remove and iterate over items, and retain to filter a collection in place based on a condition.
How to use Entry API for HashMap
Use HashMap::entry with or_insert to efficiently get or create a value for a key in a single operation.
How to use HashMap in Rust
Create a mutable HashMap, insert key-value pairs with insert(), and retrieve values using get() with a fallback.
How to Use HashMap in Rust: The Complete Guide
Create a HashMap in Rust using std::collections::HashMap, insert key-value pairs with insert, and retrieve values using get.
How to use HashSet in Rust
Use std::collections::HashSet to store unique items and check for their existence efficiently.
How to use LinkedList in Rust
Use std::collections::LinkedList to manage a doubly-linked list with constant-time insertion and removal at both ends.
How to Use LinkedList in Rust (and Why You Probably Shouldn't)
Use Rust's LinkedList for O(1) end insertions, but prefer Vec or VecDeque for better performance and cache efficiency.
How to Use polars for DataFrames in Rust
Install the polars crate via Cargo and use the prelude to create DataFrames from Series vectors.
How to Use std::collections: Choosing the Right Collection
Select Vec for ordered lists, String for text, and HashMap for fast key-value lookups in Rust.
How to Use the chunks and windows Methods on Slices
Use chunks(n) for fixed-size groups and windows(n) for overlapping sliding views on Rust slices.
How to Use VecDeque (Double-Ended Queue) in Rust
Create a VecDeque using std::collections::VecDeque and use push_front, push_back, pop_front, and pop_back to manage data from both ends.
How to use Vec in Rust
Create a dynamic list in Rust using Vec::new() or the vec! macro to store and manage collections of same-type values.
How to Use Vec<T> (Vectors) in Rust: The Complete Guide
Create a Rust vector with Vec::new() or vec![], add items with push(), and access them by index.
How to use windows and chunks on slices
Use `windows()` to iterate over overlapping sub-slices of a fixed size, and `chunks()` to iterate over non-overlapping sub-slices of a fixed size.
How to zip two Vecs together
Use Vec::iter().zip() to pair elements from two vectors into an iterator of tuples.
Performance: Vec vs LinkedList vs VecDeque — Which to Use?
Choose Vec for general lists, VecDeque for queues, and avoid LinkedList due to poor performance.
What Is the Difference Between HashMap and BTreeMap in Rust?
HashMap provides faster O(1) access with unordered keys, while BTreeMap offers O(log n) access with keys automatically sorted by their Ord implementation.
What is the difference between iter and into_iter
iter() borrows elements for reading, while into_iter() takes ownership and consumes the collection.
What is the difference between Vec and VecDeque
Vec is a single-ended growable array, while VecDeque is a double-ended queue optimized for operations at both ends.
What Is the Entry API for HashMap in Rust?
The entry API in Rust HashMaps allows efficient insertion or modification of values based on key existence in a single call.