- iter
- into_iter
Convert vector of chars to String
- The into_iter consumes the vector so we won't be able to use it again.
- The iter allows for the reusing the iterator.
examples/vectors/chars-to-string/src/main.rs
fn main() { let letters = vec!['R', 'u', 's', 't']; let name = letters.iter().collect::<String>(); println!("{name}"); let name = letters.into_iter().collect::<String>(); println!("{name}"); let text = String::from("The black cat"); println!("{:?}", text.chars()); let chars = text.chars().collect::<Vec<char>>(); println!("{chars:?}"); let text2 = text.chars().collect::<String>(); assert_eq!(text, text2); let reversed: String = text.chars().rev().collect(); println!("{text}"); println!("{reversed}"); }
Rust Rust Chars(['T', 'h', 'e', ' ', 'b', 'l', 'a', 'c', 'k', ' ', 'c', 'a', 't']) ['T', 'h', 'e', ' ', 'b', 'l', 'a', 'c', 'k', ' ', 'c', 'a', 't'] The black cat tac kcalb ehT