Type std::char

Overview

The primitive character type.

Methods

fn to_i64(self) -> i64

Convert a character into an integer.

Examples

let c = char::from_i64(80)?;
assert_eq!(c.to_i64(), 80);

Returns true if this char has the Alphabetic property.

Alphabetic is described in Chapter 4 (Character Properties) of the Unicode Standard and specified in the Unicode Character Database DerivedCoreProperties.txt.

Examples

assert!('a'.is_alphabetic());
assert!(''.is_alphabetic());

let c = '💝';
// love is many things, but it is not alphabetic
assert!(!c.is_alphabetic());

Returns true if this char satisfies either is_alphabetic() or is_numeric().

Examples

Basic usage:

assert!('٣'.is_alphanumeric());
assert!('7'.is_alphanumeric());
assert!(''.is_alphanumeric());
assert!('¾'.is_alphanumeric());
assert!(''.is_alphanumeric());
assert!('K'.is_alphanumeric());
assert!('و'.is_alphanumeric());
assert!(''.is_alphanumeric());

Returns true if this char has the general category for control codes.

Control codes (code points with the general category of Cc) are described in Chapter 4 (Character Properties) of the Unicode Standard and specified in the Unicode Character Database UnicodeData.txt.

Examples

Basic usage:

// U+009C, STRING TERMINATOR
assert!('\u{009c}'.is_control());
assert!(!'q'.is_control());

Returns true if this char has the Lowercase property.

Lowercase is described in Chapter 4 (Character Properties) of the Unicode Standard and specified in the Unicode Character Database DerivedCoreProperties.txt.

Examples

Basic usage:

assert!('a'.is_lowercase());
assert!('δ'.is_lowercase());
assert!(!'A'.is_lowercase());
assert!(!'Δ'.is_lowercase());

// The various Chinese scripts and punctuation do not have case, and so:
assert!(!''.is_lowercase());
assert!(!' '.is_lowercase());

Returns true if this char has one of the general categories for numbers.

The general categories for numbers (Nd for decimal digits, Nl for letter-like numeric characters, and No for other numeric characters) are specified in the Unicode Character Database UnicodeData.txt.

This method doesn't cover everything that could be considered a number, e.g. ideographic numbers like '三'. If you want everything including characters with overlapping purposes then you might want to use a unicode or language-processing library that exposes the appropriate character properties instead of looking at the unicode categories.

If you want to parse ASCII decimal digits (0-9) or ASCII base-N, use is_ascii_digit or is_digit instead.

Examples

Basic usage:

assert!('٣'.is_numeric());
assert!('7'.is_numeric());
assert!(''.is_numeric());
assert!('¾'.is_numeric());
assert!(''.is_numeric());
assert!(!'K'.is_numeric());
assert!(!'و'.is_numeric());
assert!(!''.is_numeric());
assert!(!''.is_numeric());

Returns true if this char has the Uppercase property.

Uppercase is described in Chapter 4 (Character Properties) of the Unicode Standard and specified in the Unicode Character Database DerivedCoreProperties.txt.

Examples

Basic usage:

assert!(!'a'.is_uppercase());
assert!(!'δ'.is_uppercase());
assert!('A'.is_uppercase());
assert!('Δ'.is_uppercase());

// The various Chinese scripts and punctuation do not have case, and so:
assert!(!''.is_uppercase());
assert!(!' '.is_uppercase());

Returns true if this char has the White_Space property.

White_Space is specified in the Unicode Character Database PropList.txt.

Examples

Basic usage:

assert!(' '.is_whitespace());

// line break
assert!('\n'.is_whitespace());

// a non-breaking space
assert!('\u{A0}'.is_whitespace());

assert!(!''.is_whitespace());
fn to_digit(self, radix: u64) -> Option

Converts a char to a digit in the given radix.

A 'radix' here is sometimes also called a 'base'. A radix of two indicates a binary number, a radix of ten, decimal, and a radix of sixteen, hexadecimal, to give some common values. Arbitrary radices are supported.

'Digit' is defined to be only the following characters:

  • 0-9
  • a-z
  • A-Z

Errors

Returns None if the char does not refer to a digit in the given radix.

Panics

Panics if given a radix larger than 36.

Examples

Basic usage:

assert_eq!('1'.to_digit(10), Some(1));
assert_eq!('f'.to_digit(16), Some(15));

Passing a non-digit results in failure:

assert_eq!('f'.to_digit(10), None);
assert_eq!('z'.to_digit(16), None);

Passing a large radix, causing a panic:

// this panics
let _ = '1'.to_digit(37);