split a string into an array of characters in rust

To split a string into an array of characters in Rust, you can use the built-in chars method of the str type. This will return an iterator over the characters of the string, which you can then collect into a vector. Here's an example:

main.rs
let s = "hello world";
let chars: Vec<char> = s.chars().collect();

println!("{:?}", chars); // prints ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
159 chars
5 lines

This code first defines a string s, and then calls the chars method on it to get an iterator over the characters. The collect method is then used to turn the iterator into a vector of characters. Finally, the vector is printed using the println macro and the {:?} format specifier to print the characters in a readable format.

related categories

gistlibby LogSnag