{

How to Convert a 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 uppercase of an 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 to_ascii_uppercase function returns 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 to_ascii_lowercase function returns 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 to_lowercase function returns lowercase of an 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?
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.