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 captures in Rust?
- How to match whitespace with a regex in Rust?
- How to replace a capture group using Rust regex?
- How to parse a file with Rust regex?
- How to get size of pointer in Rust
- How to replace strings using Rust regex?
- How to split a string with Rust regex?
- How to use negation in Rust regex?
- Regex example to match multiline string in Rust?
- How to use regex lookahead in Rust?
See more codes...