Rust Lang

How to Use Split String in the Rust Language

Strings are the building blocks in programming. It is nearly impossible to create a meaningful program without strings. Learning how to manipulate and work with strings can come in very handy.

In this article, we will take all Rustaceans into a journey and explore various methods to split strings in Rust.

Rust Split Method

The split method allows you to split a string with the defined separator. Consider the example below:

fn main() {
      let databases = "MySQL Oracle SQL_Server PostgreSQL MongoDB".split(" ");
      for db in databases {
            println!("{}", db);
      }
}

In the previous code, we use the split method to separate the strings in a space. Since the split method returns an iterator, we can get the individual string.

The resulting output is as shown:

MySQL
Oracle
SQL_Server
PostgreSQL
MongoDB

Another common use is to collect the separated strings into a vector using the Iterator::collect. An example is as shown:

let databases: Vec<&str> = “MySQL Oracle SQL_Server PostgreSQL MongoDB”.split(“ “).collect();
println!(“{:?}”, databases);

The previous code creates a vector with the separated strings as individual elements. The resulting output is as shown:

[“MySQL”, “Oracle”, “SQL_Server”, “PostgreSQL”, “MongoDB”]

You can also use the char::is_uppercase to separate a string on uppercase characters. An example is provided below:

fn main() {
      let fruits: Vec<&str> = “bananaItomatoIorangeIstrawberry”.split(char::is_uppercase).collect();
      println!({:?}, fruits);
}

In the previous example, we provide the char::is_uppercase as the separator for the split method.

The resulting output is as shown:

[“banana”, “tomato”, “orange”, “strawberry”]

If you want to separate numeric values, you can provide char::is_numeric as the separator to the split method.

Rust Split Whitespace

Another common method for separating a string is the split_whitespace method. It works similarly to the split method but only splits a string by whitespace characters.

The method also returns an iterator with the slice of the strings separated from the string.

An example code is provided below:

let databases = "MySQL\nSQL_Server\tOracle\nPostgreSQL\n".split_whitespace();
for db in databases {
      println!("{}", db)
}

The example program separates the strings on the whitespaces.

NOTE: A whitespace character is defined in the Unicode Derived Code Property. Learn more about it in the resource provided below:

https://unicode.org/reports/tr44/

Rust Split by New Lines

Rust provides us with the lines() method to split strings on a new line. An example is provided below:

let databases = "MySQL\nSQL_Server\nOracle\nPostgreSQL\n".lines();
for db in databases {
      println!("{}", db)
}

Similarly, the lines method returns an iterator with the slice of the strings.

Conclusion

In this article, we explored various methods we can use to separate strings based on multiple matching conditions. We use methods, such as the Rust split method, Rust split white space, and Rust split in new lines, in our discussion. We hope you found this article helpful.  Check the other Linux Hint articles for more tips and information.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list