Function to convert Vector of characters to String

for competitive programming and more in Rust


In the previous article, we saw that how can we convert a string to vector of characters in Rust . Now, we must be able to convert the vector back to string for various purposes like for output in CP.

So, in this article, we will see How to convert vector of characters to strings in Rust, and build a function for the same.

This article is going to be rather small because it is just 1 line function.

Convert Vector of char to String

We can simply use collect::<String>() function to convert Vector of chars to the string. Here is function for this approach

fn to_string(vec:Vec<char>) -> String{
return vec.iter().collect::<String>();

With Driver code, this looks like

// For converting vector to string
fn to_string(vec:Vec<char>) -> String{
return vec.iter().collect::<String>();
// Driver code
fn main() {
let str1 = vec!['H', 'e', 'l', 'l', 'o'];
println!("{}", to_string(str1));



In the above code, .iter() is used to traverse through the vector. See Iterator in Rust Documentation

And collect::<String>() function is used to convert an iterator into a String.


For many applications as well as competitive programming output, it is very handy to have a function to convert a vector to a string, especially for showing output. In this article, we made such a function using iter().collect() method in Rust.

Here is function for easy access

fn to_string(vec:Vec<char>) -> String{return vec.iter().collect::<String>();}

You might want to add this function to your template, if you are doing competitive programming in rust.

Thank You