rustHow to join list of strings in Rust
Joining a list of strings in Rust can be done using the join
method of the String
type. The join
method takes a Vec<&str>
as an argument and returns a String
with the elements of the Vec
joined together.
Code example:
let list_of_strings = vec!["Hello", "World"];
let joined_string = list_of_strings.join(" ");
Output
Hello World
Explanation of code parts:
let list_of_strings = vec!["Hello", "World"];
- This line creates aVec
of&str
s containing the strings "Hello" and "World".let joined_string = list_of_strings.join(" ");
- This line calls thejoin
method on theVec
of&str
s, passing in a single space as the separator. This returns aString
with the elements of theVec
joined together with a single space.
Helpful links:
More of Rust
- Hashshet example in Rust
- How to convert Rust bytes to hex?
- How to convert a Rust HashMap to a JSON string?
- How to convert a Rust HashMap to JSON?
- How to get the last element of a Rust slice?
- How to use non-capturing groups in Rust regex?
- How to use groups in a Rust regex?
- How to match the end of a line in a Rust regex?
- How to escape dots with regex in Rust?
- How to use regex to match a group in Rust?
See more codes...