DEV Community

Vinish Kapoor
Vinish Kapoor

Posted on • Updated on

How to check if character is uppercase in Rust?

In Rust, the char data type represents a Unicode Scalar Value. Unicode defines a unique numeric value for each character used in writing systems across the world. Checking if a char is uppercase in Rust is a common operation in text processing, and it can be done using a variety of approaches.

In this tutorial, we will explore different ways to check if a char is uppercase in Rust, including the use of the built-in methods and external crates. We will provide examples with explanations for each approach, and discuss the advantages and disadvantages of each.

Checking if a char is uppercase using built-in methods

Using the is_ascii_uppercase method

The is_ascii_uppercase method is a built-in method that checks if a char is an ASCII uppercase character. ASCII is a subset of Unicode, and it includes the characters used in the English language, as well as some special characters. The is_ascii_uppercase method returns a boolean value that indicates whether the char is an ASCII uppercase character or not.

Example:

fn main() {
    let my_char: char = 'A';
    let is_uppercase = my_char.is_ascii_uppercase();
    println!("Is {} uppercase? {}", my_char, is_uppercase);
}
Enter fullscreen mode Exit fullscreen mode

Output:

Is A uppercase? true
Enter fullscreen mode Exit fullscreen mode

In this example, we define a char variable named my_char with the value 'A'. We then call the is_ascii_uppercase method on the my_char variable, which returns true because 'A' is an uppercase ASCII character.

Using the is_uppercase method

The is_uppercase method is a built-in method that checks if a char is an uppercase Unicode character. This method returns a boolean value that indicates whether the char is an uppercase Unicode character or not.

Example:

fn main() {
    let my_char: char = 'Ä';
    let is_uppercase = my_char.is_uppercase();
    println!("Is {} uppercase? {}", my_char, is_uppercase);
}
Enter fullscreen mode Exit fullscreen mode

Output:

Is Ä uppercase? true
Enter fullscreen mode Exit fullscreen mode

In this example, we define a char variable named my_char with the value 'Ä'. We then call the is_uppercase method on the my_char variable, which returns true because 'Ä' is an uppercase Unicode character.

Checking if a char is uppercase using external crates

Using the Unicode Normalization crate

The Unicode Normalization crate provides a set of functions for working with Unicode strings, including checking if a char is uppercase. The crate includes a function named is_uppercase, which returns true if a char is an uppercase Unicode character, and false otherwise.

Example:

use unicode_normalization::UnicodeNormalization;

fn main() {
    let my_char: char = 'Ê';
    let is_uppercase = my_char.is_uppercase();
    println!("Is {} uppercase? {}", my_char, is_uppercase);
}
Enter fullscreen mode Exit fullscreen mode

Output:

Is Ê uppercase? true
Enter fullscreen mode Exit fullscreen mode

In this example, we first import the Unicode Normalization crate using the use statement. We then define a char variable named my_char with the value 'Ê'. We call the is_uppercase method on the my_char variable, which returns true because 'Ê' is an uppercase Unicode character.

Using the Regex crate

The Regex crate provides a set of functions for working with regular expressions, including checking if a char is uppercase. We can define a regular expression that matches uppercase characters, and use it to check if a char is uppercase.

Example:

use regex::Regex;

fn main() {
    let my_char: char = 'G';
    let uppercase_regex = Regex::new(r"^\p{Lu}$").unwrap();
    let is_uppercase = uppercase_regex.is_match(&my_char.to_string());

    println!("Is {} uppercase? {}", my_char, is_uppercase);
}
Enter fullscreen mode Exit fullscreen mode

Output:

Is G uppercase? true
Enter fullscreen mode Exit fullscreen mode

In this example, we first import the Regex crate using the use statement. We define a char variable named my_char with the value 'G'. We then define a regular expression that matches uppercase Unicode characters using the \p{Lu} pattern, which matches any uppercase letter in any language. We use the Regex::new method to create a new regular expression object, passing in the regular expression pattern as a string. Finally, we call the is_match method on the regular expression object, passing in the my_char variable converted to a string using the to_string method. The is_match method returns true because 'G' is an uppercase character. To learn how to check if character is vowel, check this article.

Advantages and Disadvantages of each approach

Using the built-in methods to check if a char is uppercase in Rust is the simplest and most efficient approach. The is_ascii_uppercase method is the fastest option, but it only works with ASCII characters. The is_uppercase method works with all Unicode characters, but it is slower than the is_ascii_uppercase method. However, these methods are limited in their capabilities, and they only work with individual chars.

Using external crates like the Unicode Normalization crate and the Regex crate provides more advanced functionality for working with Unicode characters, including checking if a char is uppercase. These crates can handle more complex use cases, such as checking if an entire string is uppercase or working with non-Latin characters. However, using external crates can add extra dependencies to your project and can make your code more complex.

Conclusion

Checking if a char is uppercase in Rust can be done using various approaches, including the built-in methods and external crates. The built-in methods provide a simple and efficient solution for working with individual chars, but they are limited in their capabilities. Using external crates like the Unicode Normalization crate and the Regex crate provides more advanced functionality for working with Unicode characters, but it can add extra dependencies to your project and make your code more complex.

Top comments (1)

Collapse
 
sloan profile image
Info Comment hidden by post author - thread only accessible via permalink
Sloan the DEV Moderator

Hey, this article seems like it may have been generated with the assistance of ChatGPT.

We allow our community members to use AI assistance when writing articles as long as they abide by our guidelines. Could you review the guidelines and edit your post to add a disclaimer?

Some comments have been hidden by the post's author - find out more