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 char
s.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…