Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
ascii

What is character.to_ascii_lowercase() in Rust?

okwudili onyejiaku

Overview

In the world of information exchange, ASCII stands for "American Standard Code for Information Interchange". It is a way of encoding character sets used for communication between computers. Other encodings include UFT-32, ISO-8859-1, etc.

The to_ascii_lowercase() method creates a copy of the value in its equivalent ASCII lowercase, thus returning this equivalent character. But if the character is not an ASCII, then nothing is done.

Syntax

character.to_ascii_lowercase()
Syntax for the to_ascii_lowercase() method

Parameters

character: This is the character we want to get its equivalent lowercase ASCII.

Return value

The lowercase equivalent character is returned if it is an ASCII, otherwise, the same value is returned.

Example

fn main(){
  // create some characters
  let char1 = 'i';
  let char2 = '❤';   // non ASCII
  let char3 = '☀';  // non ASCII
  let char4 = 'M';
  let char5 = 'k';

  // return the equivalent ASCII lowercase characters
  println!("{} in ASCII lowercase is = {}", char1, char1.to_ascii_lowercase()); 
  println!("{} in ASCII lowercase is = {}", char2, char2.to_ascii_lowercase());  
  println!("{} in ASCII lowercase is = {}", char3, char3.to_ascii_lowercase());  
  println!("{} in ASCII lowercase is = {}", char4, char4.to_ascii_lowercase());  
  println!("{} in ASCII lowercase is = {}", char5, char5.to_ascii_lowercase());
}
Convert characters to its equivalent lowercase ASCII

Explanation

  • Lines 3–7: We create some characters.
  • Lines 10–14: We obtain the ASCII lowercase character equivalent of the characters using the to_ascii_lowercase() method. In come case, we do not obtain some of the characters because they are not ASCII characters. Next, we print the characters obtained to the console.

RELATED TAGS

communitycreator
ascii
RELATED COURSES

View all Courses

Keep Exploring