How to Convert Vec to a String in Rust

Created
Modified

Using collect Function

The collect() function transforms an iterator into a collection. collect() can take anything iterable, and turn it into a relevant collection. This is one of the more powerful methods in the standard library, used in a variety of contexts. For example,

fn main() {
  let v = vec!["a", "b", "c"];
  let s: String = v.into_iter().collect();
  println!("{}", s);
}
abc

The original vector will be consumed. If you need to keep it, use v.iter().

Using from_iter Function

Creates a value from an iterator. See the following example:

fn main() {
  let v = vec!["a", "b", "c"];
  let s = String::from_iter(v);
  println!("{}", s);
}
abc

Related Tags