Rust Lang

Rust Length of a Vector

A vector refers to a container space used to store data in a re-sizeable array-like format. The size of a vector is unknown during compilation, but it can grow or shrink.

This article will explore how to work with Vectors in the Rust programming language in a beginner-friendly manner.

Rust Vectors

In Rust, a vector is denoted as Vec<T> and represented by three major parameters:

  1. The pointer to the data
  2. The length
  3. And the capacity

The data of a vector is allocated on the heap. The length refers to the number of elements stored in the vector. The capacity is the space allocated on the heap for any elements added to the vector.

If the length of the vector becomes higher than the capacity, the capacity is automatically increased and the elements reallocated.

Assume you have a vector with a capacity of 100 and a length of 0. This means that the vector is empty, but it still has room for 100 more elements.

Rust Create Vector

There are two prime ways to create a vector in Rust. The first method is to create an empty vector using the new method.

The syntax is as shown:

let vector_name : Vec<T> = Vec::new();

Where T is the data type stored by the vector. An example is as shown:

fn main() {

let vec : Vec<i32> = Vec::new();

}

The previous code creates a new empty vector of i32 data types.

The second method to create a vector is using the vec! macro. This macro allows you to declare and initialize a vector with values.

The syntax is as shown below:

let vector_name = vec![values];

You specify the values of the vector inside the square brackets, and separated by commas. An example is as shown below:

fn main() {

let vec = vec![1,2,3,4];

}

Rust Update Vector

Rust allows us to add or remove elements from a vector. We use the push and pop methods to add elements or remove an element from a vector.

Before using the push or pop methods, ensure the vector is mutable using the mut keyword.

The push method appends the provided element at the end of the vector. For example:

fn main() {

let mut vec = vec![1,2,3,4];

vec.push(5);

println!("{:?}", vec);

}

In the previous example, we add the integer 5 to the vector. Note, we set the vector to mutable.

The code above should print:

[1, 2, 3, 4, 5]

The pop method removes the last element in the vector. An example is as shown below:

fn main() {

let mut vec = vec![1,2,3,4];

vec.push(5);

println!("Before Pop: {:?}", vec);

vec.pop();

println!("After: {:?}", vec);

}

Rust Length and Capacity

We can get the length of a vector using the len() method. An example is as shown below:

let mut vec = vec![1,2,3,4];

println!("Length: {}", vec.len())

Length: 4

To get the capacity of a vector, we can use the capacity() method as shown below:

println!("Capacity: {}", vec.capacity())

Capacity: 8

Conclusion

In this beginner-friendly tutorial, we covered the fundamentals of working with vectors in Rust, such as the Rust create vectors, Rust update vectors, and the Rust length and capacity. Remember that this guide doesn’t exhaust all there is to know about vectors. Check other Linux Hint tutorials on Rust to learn more tips and information.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list