Rust Lang

Convert a Vec to String Type in Rust

A Vec is a data structure that represents a dynamically resizable, heap-allocated array. It is implemented as a contiguous memory block which stores the elements sequentially.

The capacity of a Vec is the maximum number of elements that it can hold without reallocating the memory, while the length is the number of elements that are currently stored in the Vec.

The Vec type is defined in the Rust standard library and is generic over its element type. Hence, a Vec can hold any element that implements the Copy trait which allows the Vec elements to be copied by value.

Alternatively, if the element type does not implement the Copy trait, it must implement the Clone trait which allows the elements to be cloned and stored in the Vec.

In this post, we will discuss how to convert a vector into a Rust string which allows you to perform the actions on the resulting type.

Convert a Vector to String in Rust

We can convert a Vec to a “String” type in Rust using the concat() method. We can do this by taking advantage of the into_iter and collect methods.

In Rust, the into_iter() method allows us to convert a collection into an iterator. This method is defined in types that implement the IntoIterator trait such as Vec, Strings, and arrays.

When we call the into_iter() method on a vector, it consumes the collection and returns the iterator over its elements. Hence, the ownership of the collection is transferred into the iterator, and the original collection becomes inaccessible.

On the other hand, the collect() method is used to convert an iterator into a collection. For example, we can use these methods to transform the elements of a vector into a string as demonstrated in the following example:

fn main() {
    let vec = vec!['h', 'e', 'l', 'l', 'o'];
    let string = vec.into_iter().collect::<String>();
    println!("{}", string);
}

 
In the previous example, we create a vector of characters with the [“h”, “e”, “l”, “l”, “o”] elements.

We then use the into_iter() method to create an iterator over the vector and then use the collect() method to collect the iterator into a string.

The resulting string contains the characters from the Vec which are concatenated together.

Compiling rust_json v0.1.0 (.\vec_to_str)
Finished dev [unoptimized + debuginfo] target(s) in 0.74s
Running `target\debug\vec_to_str.exe`
hello

 
As you can see from the previous output, we can return the characters from the vector as a single string.

Rust also allows us to concatenate the elements from the vector with a separator using the join() method. An example demonstration is as follows:

fn main() {
    let vec = vec!['h', 'e', 'l', 'l', 'o'];
    let string = vec
        .into_iter()
        .map(|c| c.to_string())
        .collect::<Vec<String>>()
        .join(".");
    println!("{}", string);
}

 
We use the into_iter() method to create an iterator over the vector. Then, we use the map() method to convert each char to a string.

Finally, we collect the iterator into a Vec<String>. Then, we use the join() method to join the elements of the Vec with the separator.

h.e.l.l.o

 

Conclusion

We learned how to use the into_iter() and collect() methods to convert a given vector into a Rust string.

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