Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Vec<char> to a string

Tags:

string

rust

How to convert Vec<char> to string form so that I can print it?

like image 249
user3596561 Avatar asked May 02 '14 14:05

user3596561


People also ask

How do you make a vector into a string?

Convert Vector to String using toString() function To convert elements of a Vector to Strings in R, use the toString() function. The toString() is an inbuilt R function used to produce a single character string describing an R object.


1 Answers

Use collect() on an iterator:

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

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

let s: String = v.iter().collect(); 

There is no more direct way because char is a 32-bit Unicode scalar value, and strings in Rust are sequences of bytes (u8) representing text in UTF-8 encoding. They do not map directly to sequences of chars.

like image 186
Vladimir Matveev Avatar answered Oct 21 '22 02:10

Vladimir Matveev