rustHow to join chars to string in Rust
Joining chars to string in Rust can be done using the collect()
method. The collect()
method takes an iterator of type char
and returns a String
.
Code example:
let chars = vec!['a', 'b', 'c'];
let string: String = chars.iter().collect();
Output
string
will be equal to "abc"
Explanation:
let chars = vec!['a', 'b', 'c'];
: This line creates a vector of typechar
containing the charactersa
,b
, andc
.let string: String =
: This line declares a variable of typeString
calledstring
.chars.iter()
: This line creates an iterator of typechar
from the vectorchars
..collect()
: This line collects the iterator of typechar
into aString
.
Helpful links:
More of Rust
- How to replace a capture group using Rust regex?
- How to calculate the sum of a Rust slice?
- How do I create an array of strings in Rust?
- How to replace all matches using Rust regex?
- How to use regex to match a double quote in Rust?
- Hashshet example in Rust
- How to use regex captures in Rust?
- How to convert JSON to a struct in Rust?
- How to pop an element from a Rust HashMap?
- How to convert a Rust HashMap to a JSON string?
See more codes...