Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

rust
ascii
lowercase
communitycreator

How to convert an ASCII character to permanent lowercase in Rust

okwudili onyejiaku

Overview

ASCII stands for American Standard Code for Information Interchange. It was the first encoding, among others such as the UTF-8, ISO-8859-1, and so on. It is a way to encode a character set used for communication between computers.

A character is an ASCII character if it is a number from 0 to 9, a letter A to Z, or if it's some special character. We can convert an uppercase ASCII character permanently to lowercase by using the make_ascii_lowercase() method. If the character is non-ASCII, nothing happens.

Syntax

character.make_ascii_lowercase()
The syntax for make_ascii_lowercase() method in Rust

Parameters

character: This is the character we want to convert to lowercase in Rust.

Return value

The value returned is a lowercase ASCII equivalent to the character character.

Example

fn main(){
  // create some characters
  let mut char1 = 'R';
  let mut char2 = '❤';   // non ASCII
  let mut char3 = 'K';  // non ASCII
  let mut char4 = 'A';
  let mut char5 = 'I';

  // convert to uppercase
  char1.make_ascii_lowercase();
  char2.make_ascii_lowercase();
  char3.make_ascii_lowercase();
  char4.make_ascii_lowercase();
  char5.make_ascii_lowercase();
  
  // print uppercase characters
  println!("{}", char1);  
  println!("{}", char2);  
  println!("{}", char3);  
  println!("{}", char4); 
  println!("{}", char5); 
}
Convert an ASCII character to lowercase permanently in Rust

Explanation

  • Lines 3-7: We create some mutable characters. Mutable here means changeable.
  • Lines 10-14: We convert the characters to lowercase using the make_ascii_lowercase() method.
  • Lines 17-21: We print the results to the console.

RELATED TAGS

rust
ascii
lowercase
communitycreator
RELATED COURSES

View all Courses

Keep Exploring