THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
This tutorial explains how to join a strings with separator from a Vector.
For example, We have a list of strings in a Vector object.
let numbers = vec!["One", "Two", "Three"];
You want to iterate the list of strings and join using a separator. The separator can be a blank space or hyphen.
Rust provides two methods as of version 1.56
join function
This function was added since the 1.3.0 version
pub fn join<Separator>(&self, sep: Separator) -> <Self as Join<Separator>>::Output
These methods take Separator and a string and return the join the strings
Let’s an example of joining the strings with hyphen(-)
fn main() {
let numbers = vec!["One", "Two", "Three"];
let result = numbers.join("-");
let result1 = numbers.connect("-");
println!("{}", result);
println!("{}", result1);
}
Output:
One-Two-Three
One-Two-Three
Another example is to join the strings using a blank space
fn main() {
let numbers = vec!["One", "Two", "Three"];
let result = numbers.join(" ");
let result1 = numbers.connect(" ");
println!("{}", result);
println!("{}", result1);
}
Output:
One Two Three
One Two Three
🧮 Tags
Recent posts
Three dots in react components with example|Spread operator How to update ReactJS's `create-react-app`? Difference between super and super props in React constructor class? How to check the element index of a list or array in the swift example How to calculate the sum of all numbers or object values in Array swift with exampleRelated posts