flâneur — a map of the web's best reading

Storing Lists of Values with Vectors - The Rust Programming Language

doc.rust-lang.org · 2,132 words · saved by 1 readers

The first collection type we’ll look at is Vec<T>, also known as a vector. Vectors allow you to store more than one value in a single data structure that puts all the values next to each other in memory. Vectors can only store values of the same type. They are useful when you have a list of items, such as the lines of text in a file or the prices of items in a shopping cart. To create a new empty vector, we call the Vec::new function, as shown in Listing 8-1. Listing 8-1: Creating a new, empty vector to hold values of type i32 Note that we added a type annotation here. Because we aren’t inserting any values into this vector, Rust doesn’t know what kind of elements we intend to store. This is an important point. Vectors are implemented using generics; we’ll cover how to use generics with your own types in Chapter 10. For now, know that the Vec<T> type provided by the standard library can hold any type. When we create a vector to hold a specific type, we can specify the type within angle

Storing Lists of Values with Vectors - The Rust Programming Language Keyboard shortcuts Press ← or → to navigate between chapters Press S or / to search in the book Press ? to show this help Press Esc to hide this help Auto Light Rust Coal Navy Ayu The Rust Programming Language Storing Lists of Values with Vectors The first collection type we’ll look at is Vec<T> , also known as a vector. Vectors allow you to store more than one value in a single data structure that puts all the values next to each other in memory. Vectors can only store values of the same type. They are useful when you have a

Explore this link on the map →

saved by

related reading