How to Convert Strings into Uppercase and Lower case in Rust example

This program shows you multiple ways to convert String into Uppercase in Rust with examples.

For example, if the given string is Hello, Output is HELLO for uppercase. If the given string is Hello, Output is hello for lowercase.

How Convert lower case string to Upper case in Rust?

In this, Let’s see how to convert string to upper case in Rust.

There are multiple ways to convert a string to an uppercase.

  • use str::to_uppercase function to_uppercase function returns the uppercase of a string

Here is an example program

fn main() {
    let str = "Hello World";
    println!(" {}", str.to_uppercase());
}

Output:

 HELLO WORLD
  • use str::to_ascii_uppercase function The to_ascii_uppercase function returns the uppercase of an ASCII character in a string.

ASCII characters such as a to z are converted to A to Z. Non-ASCII characters are not changed and return the same.

Here is an example program

fn main() {
    let str = "Hello World";
    println!(" {}", str.to_ascii_uppercase());
}

Output:

 HELLO WORLD

How to Convert String to Lowercase in Rust

In this, Let’s see how to convert string to lower case in Rust. Each uppercase character in a string is converted to a small case.

  • use str::to_ascii_lowercase function The to_ascii_lowercase function returns the lowercase of an ASCII character in a string.

ASCII characters such as A to Z are converted to a to z. Non-ASCII characters are not changed and return the same.

Here is an example program

fn main() {
    let str = "Hello World";
    println!(" {}", str.to_ascii_lowercase());
}

Output:

 hello world
  • use str::to_lowercase function The to_lowercase function returns the lowercase of a string.

Here is an example program

fn main() {
    let str = "HELLO WORLD";
    println!(" {}", str.to_lowercase());
}

Output:

 hello world

Conclusion

In this article, Learn multiple ways with examples on

  • How to Convert String into UPPER CASE string?
  • How to Convert String into lower case string?