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 a hyphen.
Rust provides two methods as of version 1.56
join function
This function was added in the 1.3.0 version
pub fn join<Separator>(&self, sep: Separator) -> <Self as Join<Separator>>::Output
These methods take a Separator and a string and return the join the strings
Let’s an example of joining the strings with a 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
Puppeteer Login Test example How to convert Double to Integer or Integer to double in Dart| Flutter By Example Best ways to fix 504 gateway time out in nodejs Applications Fix for Error No configuration provided for scss Multiple ways to List containers in a Docker with examplesRelated posts