rustrust string as char array
A Rust String
can be converted to a char
array using the .chars()
method. This method returns an iterator over the characters of the string.
Example code
let my_string = "Hello World!";
let char_array: Vec<char> = my_string.chars().collect();
Output example
[ 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!' ]
Code explanation
let my_string = "Hello World!";
: This line declares aString
variable calledmy_string
and assigns it the value"Hello World!"
.let char_array: Vec<char> = my_string.chars().collect();
: This line declares aVec<char>
variable calledchar_array
and assigns it the result of the.chars()
method called onmy_string
. The.chars()
method returns an iterator over the characters of the string, and the.collect()
method collects the iterator into aVec<char>
.
Helpful links
More of Rust
- How to use regex to match a group in Rust?
- How to replace a capture group using Rust regex?
- How to match whitespace with a regex in Rust?
- How to replace strings using Rust regex?
- How to match the end of a line in a Rust regex?
- How to get a capture group using Rust regex?
- How to create a new Rust HashMap with values?
- How to use regex with bytes in Rust?
- How to get an entry from a HashSet in Rust?
- How to create a HashMap of HashMaps in Rust?
See more codes...