Rust Lang

String Trimming in Rust

String trimming refers to removing the unwanted characters from a string’s beginning and end. For example, the most common use of string trimming is to remove the whitespace characters such as spaces, tabs, and newlines from the beginning and at the end of a string.

A common use case of string trimming is sanitizing the user input or preparing a string for further processing in other program operations. For example, if you’re parsing a text file, you may want to trim each line to remove any extra whitespace characters.

In this tutorial, we will discuss how we can use the built-in methods and functions in Rust to perform the string trimming to remove the whitespace characters from the beginning and/or end of a given string.

Method 1: Using the Trim() Method

As you can guess, Rust provides us with a simple and intuitive method that we can use to trim a given input string.

The first method is the trim() method. This method allows us to remove the whitespace characters from an input string’s beginning and end. The trim() method returns a new string with removed whitespace characters.

The method definition is as follows:

fn trim(&self) -> &str

 
The method takes a reference to a string (&self) and returns a reference to a new string slice (&str) with the whitespace characters being removed from both the beginning and end of the original string.

NOTE: This method does not modify the original string. Instead, it creates a new string slice that contains a portion of the original string with the whitespace characters being removed.

The following code example shows how we can use the trim() method to remove the trailing and ending whitespace characters from an input string:

fn main() {
    let s = "   hello   ".to_string();
    let trimmed = s.trim();
    println!("Original string: '{}'", s);
    println!("Trimmed string: '{}'", trimmed);
}

 
Once we run the previous code, the method should remove the whitespace characters from the string and return the output as follows:

Original string: '   hello   '
Trimmed string: 'hello'

 

Method 2: Using the Trim_Start() Method

The second method that we can use to trim a string is the trim_start() method. However, this method only allows us to trim the whitespace characters from the beginning of an input string.

An example is shown in the following:

fn main() {
    let s = "   hello  ";
    let trimmed = s.trim_start();
    println!("Original string: '{}'", s);
    println!("Trimmed string: '{}'", trimmed);
}

 
Output:

Original string: '   hello  '
Trimmed string: 'hello  '

 
As you can see, the trim_start() method removes the leading whitespace characters from the original string.

Method 3: Using the Trim_End() Method

We also have access to the trim_end() method which allows us to remove the whitespace characters from the end of a string.

Example:

fn main() {
    let s = "   hello  ";
    let trimmed = s.trim_end();
    println!("Original string: '{}'", s);
    println!("Trimmed string: '{}'", trimmed);
}

 
Output:

Original string: '   hello  '
Trimmed string: '   hello'

 

Conclusion

As you discovered in this tutorial, using the trim(), trim_start(), and trim_end() methods in Rust are beneficial to remove the whitespace characters from the strings. Whether you need to remove the leading or trailing whitespace or both ends, these methods provide an efficient and easy-to-use solution.

 

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