Introduction

In Rust, converting a string to lower case can be done using the to_lowercase() method. The to_lowercase() method returns a new String with all the characters converted to lower case. Below is a simple example of how to use this method:

fn main() {
    let my_string = String::from(""Hello, World!"");
    let lowercased_string = my_string.to_lowercase();

    println!(""Original String: {}"", my_string);
    println!(""Lowercased String: {}"", lowercased_string);
}

Output:

Original String: Hello, World!
Lowercased String: hello, world!

As you can see, the to_lowercase() method converts all uppercase characters in the string to their corresponding lowercase equivalents. It doesn’t modify the original string but instead returns a new lowercased string.

It is important to note that this method considers the Unicode case mappings when converting characters to lowercase. This means that characters like ““İ”” (Latin capital letter I with dot above) will be converted to ““i”” in lowercase, even though the ASCII representation of ““İ”” is ““I””.

If you only want to convert ASCII characters to lowercase, you can use the to_ascii_lowercase() method instead:

fn main() {
    let my_string = String::from(""Hello, World!"");
    let lowercased_string = my_string.to_ascii_lowercase();

    println!(""Original String: {}"", my_string);
    println!(""Lowercased String (ASCII): {}"", lowercased_string);
}

Output:

Original String: Hello, World!
Lowercased String (ASCII): hello, world!

The to_ascii_lowercase() method only converts characters in the string that have an ASCII representation. Non-ASCII characters remain unchanged.

I hope you found this tutorial helpful!