Skip to main content

8 docs tagged with "collections"

View all tags

Common Collection Methods

We've now learned about the three most common collection types in Rust: Vec, String, and HashMap. While each has its own specific use case, they share a common foundation through the Iterator trait. This means they have a rich set of methods for performing common operations like sorting, filtering, and mapping. In this article, we'll explore some of these useful methods.

Iterating over Collections: The `Iterator` Trait

We've seen how to use for loops to iterate over collections. But how does it actually work? The magic behind for loops is the Iterator trait. In this article, we'll take a closer look at this trait and the different ways you can create an iterator from a collection.

Project: To-Do List

To wrap up our series on collections, we're going to build one more project: a simple command-line to-do list application. This project will primarily use a Vec to store the to-do items and will allow the user to add items, list items, and mark items as complete.

Project: Word Counter

It's time to put everything we've learned about collections into practice. In this project, we will build a program that counts the occurrences of each word in a given text. This is a classic programming exercise that is a perfect way to use the HashMap to solve a real-world problem.

Slices: A View into a Collection

We've learned about several collection types like String and Vec. A slice is a different kind of collection. It doesn't have ownership of its data. Instead, a slice is a reference to a contiguous sequence of elements in another collection. This allows you to work with a part of a collection without having to copy the data.

Storing UTF-8 Text with Strings (`String`)

We've already touched on Rust's two main string types, &str and String. In this article, we're going to take a deeper dive into the String type, which is a growable, mutable, owned, UTF-8 encoded string type. It is one of the most common and useful collection types in Rust.

Vectors (`Vec<T>`): The Growable Array

Welcome to our fifth series! Now that you have a solid understanding of Rust's basic data types and control flow, it's time to dive into collections. Collections are data structures that can contain multiple values. Unlike the array and tuple types we've seen before, some collection types can grow or shrink in size.