How to Join Vector of Strings in Rust
1. Overview
In Rust, there are situations where we need to join elements in a Vector into a single string. To achieve this, the Rust standard library provides join() and connect() operators. However, the connect() has been deprecated in favor of join().
In this short tutorial, we’ll learn how to join vec of String type together using both the connect() and join() functions.
2. Using the connect() Function
Prior to the Rust 1.3.0 version, we could combine all elements inside a Vector into a single output using the connect() function, with a separator inserted between the elements.
Here’s an example that uses the connect() to combine the Vector of strings:
let words = vec!["Rust","World"];
assert_eq!(words.connect("-"), "Rust-World");
In the code above, we define a variable of Vec type named words. The vector has two elements. Next, we invoke the connect() function on words, accepting a separator (-) as its argument. In this case, the separator is a hyphen.
Notably, the separator can be any value:
assert_eq!(words.connect(" "), "Rust World");
Here, we use a single space as the separator and assert that we get the expected output.
3. Using the join() Function
The connect() function was renamed to join() since Rust version 1.3.0 though the connect() function is still available in the standard library for backward compatibility. Like the connect() function, we can use join() to flatten a Vector of strings into a single output:
let words = vec!["Rust","World"];
assert_eq!(words.join("-"), "Rust-World");
In the code above, we use the join() function to combine elements in words, using a hyphen as the separator.
It’s recommended that the join() function be used since it’s intended to replace connect().
4. Performance Test
Furthermore, to compare the performance of connect() and join(), let’s write a simple benchmark test using the nightly Rust version:
extern crate test;
use test::Bencher;
fn strings_vec() -> Vec {
vec![String::from("Rustling"); 512]
}
#[bench]
fn bench_join(b: &mut Bencher) {
let words = strings_vec();
b.iter(|| {
let _ = words.join("-");
});
}
#[bench]
fn bench_connect(b: &mut Bencher) {
let words: Vec = strings_vec();
b.iter(|| {
let _ = words.connect("-");
});
}
This benchmark combines 512 strings repeatedly using both join() and connect(). Let’s compile the benchmark using the cargo bench command.
Notably, this benchmark was run with the nightly Rust version.
Here’s the benchmark result:
test joinoperator::bench_connect bench: 3,233.14 ns/iter (+/- 403.33)
test joinoperator::bench_join bench: 3,215.61 ns/iter (+/- 404.73)
The results show that both methods perform similarly, with no significant advantage for either.
5. Conclusion
In this short tutorial, we learned how to use the connect() and join() functions to combine elements of a vector into a single string. Also, we understand that the connect() function is deprecated, and the join() function is the recommended method for modern Rust
Lastly, we performed a benchmark test that demonstrated similar performance for both methods.
As usual, the source code for the example is available on GitHub.