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 double quote in Rust?
- How to use regex to match a group in Rust?
- How to parse JSON string in Rust?
- How to use a HashBrown with a Rust HashMap?
- How to replace a capture group using Rust regex?
- How to replace all matches using Rust regex?
- How to convert a Rust slice to a fixed array?
- How to map an array in Rust
- How to split a string with Rust regex?
- How to use non-capturing groups in Rust regex?
See more codes...