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 aStringvariable calledmy_stringand assigns it the value"Hello World!".let char_array: Vec<char> = my_string.chars().collect();: This line declares aVec<char>variable calledchar_arrayand 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
- Regex example to match multiline string in Rust?
- How to map a Rust slice?
- How to replace a capture group using Rust regex?
- How to use regex captures in Rust?
- How to create a HashMap of structs in Rust?
- How to use non-capturing groups in Rust regex?
- How to match the end of a line in a Rust regex?
- How to use regex with bytes in Rust?
- How to perform matrix operations in Rust?
- How to use regex lookbehind in Rust?
See more codes...