Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Rust Data Structures


Data Structures

In Rust, data structures are used to store and organize values.

Rust provides many built-in data structures. Each is used to handle data in different ways.

Some of the most common are:

  • Array
  • Vector (Vec)
  • Tuple
  • HashMap

We will explore all of them in detail later, but for now, here's a quick introduction to each one.


Arrays

An array in Rust is a fixed-size list of values, all of the same type.

You cannot grow or shrink an array after it's created.

To access an array element, refer to its index number.

Array indexes start with 0: [0] is the first element, [1] is the second element, etc.

Example

let fruits = ["apple", "banana", "orange"];
println!("Last fruit: {}", fruits[2]);
Try it Yourself »

Vectors

A vector is a resizable array. Unlike regular arrays, vectors can grow or shrink in size.

Example

let mut fruits = vec!["apple", "banana"];
fruits.push("cherry");

println!("Last fruit: {}", fruits[2]);
Try it Yourself »

Tuples

A tuple can hold multiple values of different types. It is useful when grouping different types together.

You access tuple elements using a dot and an index number, like person.1, etc:

Example

let person = ("John", 30, true);
println!("Name: {}", person.0);
println!("Age: {}", person.1);
println!("Is active: {}", person.2);
Try it Yourself »

HashMaps

A HashMap stores key-value pairs. It lets you look up a value using a key.

To use HashMap, you must import it from the standard library.

Example

// Import HashMap
use std::collections::HashMap;

fn main() {
  let mut capitalCities = HashMap::new();
  capitalCities.insert("France", "Paris");
  capitalCities.insert("Japan", "Tokyo");

  println!("Capital of Japan is {}", capitalCities["Japan"]);
}
Try it Yourself »

Data Structures Overview

Data Structure Use Case Can Grow?
Array Fixed-size list of same-type values No
Vector (Vec) Growable list of same-type values Yes
Tuple Group different types together No
HashMap Key-value lookup Yes

Next, let's take a closer look at each data structure in more detail.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.