Regardless of the programming language, splitting a string is a common practice that programmers, developers, and software engineers will have to do at some point during their careers. In this article, we are going to explain how to split a string using Rust programming language, and the different alternatives available.
Note: While the main question is to split a string in Rust, this article presents different ways to split string slices. Do not confuse a string with a string slice.
Table of Contents
How to Split a String Slice Using the split() Method
To split a string slice or type &str
in Rust, use the split() method to create an iterator. Once the iterator is generated, use a for
loop to access each substring to apply any additional business logic. Let’s look at this implementation in the following snippet of code.
use std::str::Split;
fn main() {
let text: &str = "Manchester Bogota Paris Dallas Chicago";
let cities: Split<&str> = text.split(" ");
for city in cities {
println!("City {}", city);
}
}
// or
fn main() {
let cities: Split<&str> = "Manchester Bogota Paris Dallas Chicago".split(" ");
for city in cities {
println!("City {}", city);
}
}
// Output
// City Manchester
// City Bogota
// City Paris
// City Dallas
// City Chicago
Notice how we logged each city after splitting them by a space character " "
. The split() method can accept another separator as an argument besides the space character. For instance, we could extract the sentences of a paragraph by separating them with a period"."
.
fn main() {
let paragraphs = "Lorem ipsum dolor. Ut enim ad minim veniam. Duis aute irure . Excepteur sint.".split(".");
for paragraph in paragraphs {
println!("Paragraph {}", paragraph);
}
}
// Output
// Paragraph Lorem ipsum dolor
// Paragraph Ut enim ad minim veniam
// Paragraph Duis aute irure
// Paragraph Excepteur sint
// Paragraph
Unfortunately, the last example would introduce a logical error as it displays an empty string for the last element. To quickly fix this, check for empty strings by leveraging the is_empty() method on the string slice paragraph
.
fn main() {
let paragraphs =
"Lorem ipsum dolor. Ut enim ad minim veniam. Duis aute irure . Excepteur sint.".split(".");
for paragraph in paragraphs {
if (!paragraph.is_empty()) {
println!("Paragraph {}", paragraph);
}
}
}
How to Split a String Slice Using the split_whitespace() Method
In the previous example, we used the split() method to split a string slice separated by spaces. Rust provides a built-in method that does the same, without the need of passing whitespace as a parameter, called split_whitespace().
use std::str::SplitWhitespace;
fn main() {
let cities: SplitWhitespace = "Manchester Bogota Paris Dallas Chicago".split_whitespace();
for city in cities {
println!("City {}", city);
}
}
Note: In case you decide to update from using the split() method to using the split_whitespace() method, make sure to also update the type of the variable, in our case cities
to SplitWhiteSpace
.
How to Split a String Slice Using the split_terminator() Method
Remember when we used the split() method to split sentences in a paragraph? We had to apply additional logic to check for empty substrings after using the split method. This is what the solution looked like.
fn main() {
let paragraphs =
"Lorem ipsum dolor. Ut enim ad minim veniam. Duis aute irure . Excepteur sint.".split(".");
for paragraph in paragraphs {
if (!paragraph.is_empty()) {
println!("Paragraph {}", paragraph);
}
}
}
Notice how it checks whether the substring paragraph
is empty prior to printing it in the terminal. We can avoid this check by using the split_terminator() method instead of the split() method.
The split_terminator() method is the equivalent to the split method, except that substrings are skipped if they are empty. Hence, by using the split_terminator() method, we no longer have to check for empty strings. Let’s look at the new implementation of the code.
fn main() {
let paragraphs =
"Lorem ipsum dolor. Ut enim ad minim veniam. Duis aute irure . Excepteur sint.".split_terminator(".");
for paragraph in paragraphs {
println!("Paragraph {}", paragraph);
}
}
How to Generate a Growable Array After Splitting a String Slice
In case you don’t want to use a for
loop, but instead, generate a collection or a growable array (Vec<&str>
), apply the split() method on the string slice followed by the collect() method once the iterator is generated.
fn main() {
let text: &str = "Manchester Bogota Paris Dallas Chicago";
let cities: Split<&str> = text.split(" ");
let array_cities: Vec<&str> = cities.collect();
}
// or
fn main() {
let array_cities: Vec<&str> = "Manchester Bogota Paris Dallas Chicago"
.split(" ")
.collect();
}
Conclusion
While it is common to use the split() method to split a string slice, there are other built-in methods available that already simplify the job such as using split_whitespace(), in case we split by whitespace, or using the split_terminator(), in case we want to skip empty substrings.
Was this article helpful?
I hope this article helped you find a definite answer to such a common task as splitting a string in Rust.
Share your thoughts by replying on Twitter of Become A Better Programmer or to my personal Twitter account.