Day 13a: Introduction to Vectors in Rust – Basics, Syntax, and Dynamic Nature

Venkat Annangi
Venkat Annangi
01/10/2024 04:10 3 min read 72 views
#rust-vectors #rust #108 days of rust

Day 13a: Introduction to Vectors in Rust – Basics, Syntax, and Dynamic Nature

Welcome to Day 12 of our journey through Rust! Today, we’re delving into vectors, which are among the most versatile and widely used data structures in Rust. Unlike arrays, vectors are dynamic, meaning they can grow and shrink in size as needed. This flexibility makes vectors ideal for working with collections of data whose size isn’t known at compile time.

1. What is a Vector in Rust?

A vector in Rust is a resizable array-like data structure that can hold multiple values of the same type. Vectors are part of the Rust standard library and offer many useful methods for manipulating collections of data.

Vectors are defined using the Vec<T> type, where T represents the type of elements stored in the vector.

Example of a Vector:

fn main() {
    let mut numbers: Vec<i32> = Vec::new(); // Create an empty vector of i32
    numbers.push(1); // Add elements to the vector
    numbers.push(2);
    numbers.push(3);

    println!("Vector: {:?}", numbers);
  }

2. Declaring Vectors

Vectors can be declared in multiple ways, depending on whether you want to create an empty vector or initialize it with values.

a. Creating an Empty Vector

You can create an empty vector using Vec::new() or the vec![] macro.

Example:

fn main() {
    let v1: Vec<i32> = Vec::new(); // Empty vector of type i32
    let v2 = vec![]; // Empty vector using the macro
  }

b. Creating a Vector with Initial Values

You can create a vector with initial values using the vec![] macro, specifying the elements directly inside the brackets.

Example:

fn main() {
    let fruits = vec!["apple", "banana", "cherry"]; // Vector of string slices
    println!("Fruits: {:?}", fruits);
  }

3. Differences Between Arrays and Vectors

While arrays and vectors might seem similar, there are key differences between them:

  • Fixed vs. Dynamic Size: Arrays have a fixed size determined at compile time, whereas vectors are dynamic and can grow or shrink as needed.
  • Memory Allocation: Arrays are allocated on the stack (for smaller sizes), whereas vectors are allocated on the heap, allowing them to expand beyond a fixed capacity.
  • Usage: Use arrays when the number of elements is known and doesn’t change. Use vectors when you need flexibility to add or remove elements.

4. Common Operations with Vectors

a. Adding Elements

You can add elements to a vector using the push() method, which appends the value to the end of the vector.

Example:

fn main() {
    let mut numbers = vec![1, 2, 3];
    numbers.push(4); // Add an element to the end

    println!("Updated vector: {:?}", numbers);
  }

b. Accessing Elements

Elements in a vector can be accessed by index, just like an array. Note that indexing starts at zero.

Example:

fn main() {
    let cities = vec!["New York", "Paris", "Tokyo"];
    let city = &cities[1]; // Access the element at index 1

    println!("City: {}", city);
  }

Conclusion

Vectors are an essential part of Rust's standard library, offering flexibility and dynamic behavior that arrays lack. Today, we covered the basics of declaring vectors, adding and accessing elements, and the key differences between arrays and vectors. By understanding the dynamic nature of vectors, you can make more informed decisions about which data structure is best suited for your particular use case.

In the next post, Day 13b, we’ll explore how to add, remove, and access elements in vectors using various methods, along with examples and best practices to avoid common pitfalls.

Comments