Enum AsciiChar

enum AsciiChar

One of the 128 Unicode characters from U+0000 through U+007F, often known as the ASCII subset.

Officially, this is the first block in Unicode, Basic Latin. For details, see the C0 Controls and Basic Latin code chart.

This block was based on older 7-bit character code standards such as ANSI X3.4-1977, ISO 646-1973, and NIST FIPS 1-2.

When to use this

The main advantage of this subset is that it's always valid UTF-8. As such, the &[ascii::Char] -> &str conversion function (as well as other related ones) are O(1): no runtime checks are needed.

If you're consuming strings, you should usually handle Unicode and thus accept strs, not limit yourself to ascii::Chars.

However, certain formats are intentionally designed to produce ASCII-only output in order to be 8-bit-clean. In those cases, it can be simpler and faster to generate ascii::Chars instead of dealing with the variable width properties of general UTF-8 encoded strings, while still allowing the result to be used freely with other Rust things that deal in general strs.

For example, a UUID library might offer a way to produce the string representation of a UUID as an [ascii::Char; 36] to avoid memory allocation yet still allow it to be used as UTF-8 via as_str without paying for validation (or needing unsafe code) the way it would if it were provided as a [u8; 36].

Layout

This type is guaranteed to have a size and alignment of 1 byte.

Names

The variants on this type are Unicode names of the characters in upper camel case, with a few tweaks:

Variants

Null

U+0000 (The default variant)

StartOfHeading

U+0001

StartOfText

U+0002

EndOfText

U+0003

EndOfTransmission

U+0004

Enquiry

U+0005

Acknowledge

U+0006

Bell

U+0007

Backspace

U+0008

CharacterTabulation

U+0009

LineFeed

U+000A

LineTabulation

U+000B

FormFeed

U+000C

CarriageReturn

U+000D

ShiftOut

U+000E

ShiftIn

U+000F

DataLinkEscape

U+0010

DeviceControlOne

U+0011

DeviceControlTwo

U+0012

DeviceControlThree

U+0013

DeviceControlFour

U+0014

NegativeAcknowledge

U+0015

SynchronousIdle

U+0016

EndOfTransmissionBlock

U+0017

Cancel

U+0018

EndOfMedium

U+0019

Substitute

U+001A

Escape

U+001B

InformationSeparatorFour

U+001C

InformationSeparatorThree

U+001D

InformationSeparatorTwo

U+001E

InformationSeparatorOne

U+001F

Space

U+0020

ExclamationMark

U+0021

QuotationMark

U+0022

NumberSign

U+0023

DollarSign

U+0024

PercentSign

U+0025

Ampersand

U+0026

Apostrophe

U+0027

LeftParenthesis

U+0028

RightParenthesis

U+0029

Asterisk

U+002A

PlusSign

U+002B

Comma

U+002C

HyphenMinus

U+002D

FullStop

U+002E

Solidus

U+002F

Digit0

U+0030

Digit1

U+0031

Digit2

U+0032

Digit3

U+0033

Digit4

U+0034

Digit5

U+0035

Digit6

U+0036

Digit7

U+0037

Digit8

U+0038

Digit9

U+0039

Colon

U+003A

Semicolon

U+003B

LessThanSign

U+003C

EqualsSign

U+003D

GreaterThanSign

U+003E

QuestionMark

U+003F

CommercialAt

U+0040

CapitalA

U+0041

CapitalB

U+0042

CapitalC

U+0043

CapitalD

U+0044

CapitalE

U+0045

CapitalF

U+0046

CapitalG

U+0047

CapitalH

U+0048

CapitalI

U+0049

CapitalJ

U+004A

CapitalK

U+004B

CapitalL

U+004C

CapitalM

U+004D

CapitalN

U+004E

CapitalO

U+004F

CapitalP

U+0050

CapitalQ

U+0051

CapitalR

U+0052

CapitalS

U+0053

CapitalT

U+0054

CapitalU

U+0055

CapitalV

U+0056

CapitalW

U+0057

CapitalX

U+0058

CapitalY

U+0059

CapitalZ

U+005A

LeftSquareBracket

U+005B

ReverseSolidus

U+005C

RightSquareBracket

U+005D

CircumflexAccent

U+005E

LowLine

U+005F

GraveAccent

U+0060

SmallA

U+0061

SmallB

U+0062

SmallC

U+0063

SmallD

U+0064

SmallE

U+0065

SmallF

U+0066

SmallG

U+0067

SmallH

U+0068

SmallI

U+0069

SmallJ

U+006A

SmallK

U+006B

SmallL

U+006C

SmallM

U+006D

SmallN

U+006E

SmallO

U+006F

SmallP

U+0070

SmallQ

U+0071

SmallR

U+0072

SmallS

U+0073

SmallT

U+0074

SmallU

U+0075

SmallV

U+0076

SmallW

U+0077

SmallX

U+0078

SmallY

U+0079

SmallZ

U+007A

LeftCurlyBracket

U+007B

VerticalLine

U+007C

RightCurlyBracket

U+007D

Tilde

U+007E

Delete

U+007F

Implementations

impl AsciiChar

const fn from_u8(b: u8) -> Option<Self>

Creates an ASCII character from the byte b, or returns None if it's too large.

unsafe const fn from_u8_unchecked(b: u8) -> Self

Creates an ASCII character from the byte b, without checking whether it's valid.

Safety

b must be in 0..=127, or else this is UB.

const fn digit(d: u8) -> Option<Self>

When passed the number 0, 1, …, 9, returns the character '0', '1', …, '9' respectively.

If d >= 10, returns None.

unsafe const fn digit_unchecked(d: u8) -> Self

When passed the number 0, 1, …, 9, returns the character '0', '1', …, '9' respectively, without checking that it's in-range.

Safety

This is immediate UB if called with d > 64.

If d >= 10 and d <= 64, this is allowed to return any value or panic. Notably, it should not be expected to return hex digits, or any other reasonable extension of the decimal digits.

(This loose safety condition is intended to simplify soundness proofs when writing code using this method, since the implementation doesn't need something really specific, not to make those other arguments do something useful. It might be tightened before stabilization.)

const fn to_u8(self: Self) -> u8

Gets this ASCII character as a byte.

const fn to_char(self: Self) -> char

Gets this ASCII character as a char Unicode Scalar Value.

const fn as_str(self: &Self) -> &str

Views this ASCII character as a one-code-unit UTF-8 str.

const fn to_uppercase(self: Self) -> Self

Makes a copy of the value in its upper case equivalent.

Letters 'a' to 'z' are mapped to 'A' to 'Z'.

To uppercase the value in-place, use make_uppercase.

Examples

#![feature(ascii_char, ascii_char_variants)]
use std::ascii;

let lowercase_a = ascii::Char::SmallA;

assert_eq!(
    ascii::Char::CapitalA,
    lowercase_a.to_uppercase(),
);
const fn to_lowercase(self: Self) -> Self

Makes a copy of the value in its lower case equivalent.

Letters 'A' to 'Z' are mapped to 'a' to 'z'.

To lowercase the value in-place, use make_lowercase.

Examples

#![feature(ascii_char, ascii_char_variants)]
use std::ascii;

let uppercase_a = ascii::Char::CapitalA;

assert_eq!(
    ascii::Char::SmallA,
    uppercase_a.to_lowercase(),
);
const fn eq_ignore_case(self: Self, other: Self) -> bool

Checks that two values are a case-insensitive match.

This is equivalent to to_lowercase(a) == to_lowercase(b).

Examples

#![feature(ascii_char, ascii_char_variants)]
use std::ascii;

let lowercase_a = ascii::Char::SmallA;
let uppercase_a = ascii::Char::CapitalA;

assert!(lowercase_a.eq_ignore_case(uppercase_a));
const fn make_uppercase(self: &mut Self)

Converts this value to its upper case equivalent in-place.

Letters 'a' to 'z' are mapped to 'A' to 'Z'.

To return a new uppercased value without modifying the existing one, use to_uppercase.

Examples

#![feature(ascii_char, ascii_char_variants)]
use std::ascii;

let mut letter_a = ascii::Char::SmallA;

letter_a.make_uppercase();

assert_eq!(ascii::Char::CapitalA, letter_a);
const fn make_lowercase(self: &mut Self)

Converts this value to its lower case equivalent in-place.

Letters 'A' to 'Z' are mapped to 'a' to 'z'.

To return a new lowercased value without modifying the existing one, use to_lowercase.

Examples

#![feature(ascii_char, ascii_char_variants)]
use std::ascii;

let mut letter_a = ascii::Char::CapitalA;

letter_a.make_lowercase();

assert_eq!(ascii::Char::SmallA, letter_a);
const fn is_alphabetic(self: Self) -> bool

Checks if the value is an alphabetic character:

  • 0x41 'A' ..= 0x5A 'Z', or
  • 0x61 'a' ..= 0x7A 'z'.

Examples

#![feature(ascii_char, ascii_char_variants)]
use std::ascii;

let uppercase_a = ascii::Char::CapitalA;
let uppercase_g = ascii::Char::CapitalG;
let a = ascii::Char::SmallA;
let g = ascii::Char::SmallG;
let zero = ascii::Char::Digit0;
let percent = ascii::Char::PercentSign;
let space = ascii::Char::Space;
let lf = ascii::Char::LineFeed;
let esc = ascii::Char::Escape;

assert!(uppercase_a.is_alphabetic());
assert!(uppercase_g.is_alphabetic());
assert!(a.is_alphabetic());
assert!(g.is_alphabetic());
assert!(!zero.is_alphabetic());
assert!(!percent.is_alphabetic());
assert!(!space.is_alphabetic());
assert!(!lf.is_alphabetic());
assert!(!esc.is_alphabetic());
const fn is_uppercase(self: Self) -> bool

Checks if the value is an uppercase character: 0x41 'A' ..= 0x5A 'Z'.

Examples

#![feature(ascii_char, ascii_char_variants)]
use std::ascii;

let uppercase_a = ascii::Char::CapitalA;
let uppercase_g = ascii::Char::CapitalG;
let a = ascii::Char::SmallA;
let g = ascii::Char::SmallG;
let zero = ascii::Char::Digit0;
let percent = ascii::Char::PercentSign;
let space = ascii::Char::Space;
let lf = ascii::Char::LineFeed;
let esc = ascii::Char::Escape;

assert!(uppercase_a.is_uppercase());
assert!(uppercase_g.is_uppercase());
assert!(!a.is_uppercase());
assert!(!g.is_uppercase());
assert!(!zero.is_uppercase());
assert!(!percent.is_uppercase());
assert!(!space.is_uppercase());
assert!(!lf.is_uppercase());
assert!(!esc.is_uppercase());
const fn is_lowercase(self: Self) -> bool

Checks if the value is a lowercase character: 0x61 'a' ..= 0x7A 'z'.

Examples

#![feature(ascii_char, ascii_char_variants)]
use std::ascii;

let uppercase_a = ascii::Char::CapitalA;
let uppercase_g = ascii::Char::CapitalG;
let a = ascii::Char::SmallA;
let g = ascii::Char::SmallG;
let zero = ascii::Char::Digit0;
let percent = ascii::Char::PercentSign;
let space = ascii::Char::Space;
let lf = ascii::Char::LineFeed;
let esc = ascii::Char::Escape;

assert!(!uppercase_a.is_lowercase());
assert!(!uppercase_g.is_lowercase());
assert!(a.is_lowercase());
assert!(g.is_lowercase());
assert!(!zero.is_lowercase());
assert!(!percent.is_lowercase());
assert!(!space.is_lowercase());
assert!(!lf.is_lowercase());
assert!(!esc.is_lowercase());
const fn is_alphanumeric(self: Self) -> bool

Checks if the value is an alphanumeric character:

  • 0x41 'A' ..= 0x5A 'Z', or
  • 0x61 'a' ..= 0x7A 'z', or
  • 0x30 '0' ..= 0x39 '9'.

Examples

#![feature(ascii_char, ascii_char_variants)]
use std::ascii;

let uppercase_a = ascii::Char::CapitalA;
let uppercase_g = ascii::Char::CapitalG;
let a = ascii::Char::SmallA;
let g = ascii::Char::SmallG;
let zero = ascii::Char::Digit0;
let percent = ascii::Char::PercentSign;
let space = ascii::Char::Space;
let lf = ascii::Char::LineFeed;
let esc = ascii::Char::Escape;

assert!(uppercase_a.is_alphanumeric());
assert!(uppercase_g.is_alphanumeric());
assert!(a.is_alphanumeric());
assert!(g.is_alphanumeric());
assert!(zero.is_alphanumeric());
assert!(!percent.is_alphanumeric());
assert!(!space.is_alphanumeric());
assert!(!lf.is_alphanumeric());
assert!(!esc.is_alphanumeric());
const fn is_digit(self: Self) -> bool

Checks if the value is a decimal digit: 0x30 '0' ..= 0x39 '9'.

Examples

#![feature(ascii_char, ascii_char_variants)]
use std::ascii;

let uppercase_a = ascii::Char::CapitalA;
let uppercase_g = ascii::Char::CapitalG;
let a = ascii::Char::SmallA;
let g = ascii::Char::SmallG;
let zero = ascii::Char::Digit0;
let percent = ascii::Char::PercentSign;
let space = ascii::Char::Space;
let lf = ascii::Char::LineFeed;
let esc = ascii::Char::Escape;

assert!(!uppercase_a.is_digit());
assert!(!uppercase_g.is_digit());
assert!(!a.is_digit());
assert!(!g.is_digit());
assert!(zero.is_digit());
assert!(!percent.is_digit());
assert!(!space.is_digit());
assert!(!lf.is_digit());
assert!(!esc.is_digit());
const fn is_octdigit(self: Self) -> bool

Checks if the value is an octal digit: 0x30 '0' ..= 0x37 '7'.

Examples

#![feature(ascii_char, ascii_char_variants)]

use std::ascii;

let uppercase_a = ascii::Char::CapitalA;
let a = ascii::Char::SmallA;
let zero = ascii::Char::Digit0;
let seven = ascii::Char::Digit7;
let eight = ascii::Char::Digit8;
let percent = ascii::Char::PercentSign;
let lf = ascii::Char::LineFeed;
let esc = ascii::Char::Escape;

assert!(!uppercase_a.is_octdigit());
assert!(!a.is_octdigit());
assert!(zero.is_octdigit());
assert!(seven.is_octdigit());
assert!(!eight.is_octdigit());
assert!(!percent.is_octdigit());
assert!(!lf.is_octdigit());
assert!(!esc.is_octdigit());
const fn is_hexdigit(self: Self) -> bool

Checks if the value is a hexadecimal digit:

  • 0x30 '0' ..= 0x39 '9', or
  • 0x41 'A' ..= 0x46 'F', or
  • 0x61 'a' ..= 0x66 'f'.

Examples

#![feature(ascii_char, ascii_char_variants)]
use std::ascii;

let uppercase_a = ascii::Char::CapitalA;
let uppercase_g = ascii::Char::CapitalG;
let a = ascii::Char::SmallA;
let g = ascii::Char::SmallG;
let zero = ascii::Char::Digit0;
let percent = ascii::Char::PercentSign;
let space = ascii::Char::Space;
let lf = ascii::Char::LineFeed;
let esc = ascii::Char::Escape;

assert!(uppercase_a.is_hexdigit());
assert!(!uppercase_g.is_hexdigit());
assert!(a.is_hexdigit());
assert!(!g.is_hexdigit());
assert!(zero.is_hexdigit());
assert!(!percent.is_hexdigit());
assert!(!space.is_hexdigit());
assert!(!lf.is_hexdigit());
assert!(!esc.is_hexdigit());
const fn is_punctuation(self: Self) -> bool

Checks if the value is a punctuation character:

  • 0x21 ..= 0x2F ! " # $ % & ' ( ) * + , - . /, or
  • 0x3A ..= 0x40 : ; < = > ? @, or
  • 0x5B ..= 0x60 [ \ ] ^ _ `, or
  • 0x7B ..= 0x7E { | } ~

Examples

#![feature(ascii_char, ascii_char_variants)]
use std::ascii;

let uppercase_a = ascii::Char::CapitalA;
let uppercase_g = ascii::Char::CapitalG;
let a = ascii::Char::SmallA;
let g = ascii::Char::SmallG;
let zero = ascii::Char::Digit0;
let percent = ascii::Char::PercentSign;
let space = ascii::Char::Space;
let lf = ascii::Char::LineFeed;
let esc = ascii::Char::Escape;

assert!(!uppercase_a.is_punctuation());
assert!(!uppercase_g.is_punctuation());
assert!(!a.is_punctuation());
assert!(!g.is_punctuation());
assert!(!zero.is_punctuation());
assert!(percent.is_punctuation());
assert!(!space.is_punctuation());
assert!(!lf.is_punctuation());
assert!(!esc.is_punctuation());
const fn is_graphic(self: Self) -> bool

Checks if the value is a graphic character: 0x21 '!' ..= 0x7E '~'.

Examples

#![feature(ascii_char, ascii_char_variants)]
use std::ascii;

let uppercase_a = ascii::Char::CapitalA;
let uppercase_g = ascii::Char::CapitalG;
let a = ascii::Char::SmallA;
let g = ascii::Char::SmallG;
let zero = ascii::Char::Digit0;
let percent = ascii::Char::PercentSign;
let space = ascii::Char::Space;
let lf = ascii::Char::LineFeed;
let esc = ascii::Char::Escape;

assert!(uppercase_a.is_graphic());
assert!(uppercase_g.is_graphic());
assert!(a.is_graphic());
assert!(g.is_graphic());
assert!(zero.is_graphic());
assert!(percent.is_graphic());
assert!(!space.is_graphic());
assert!(!lf.is_graphic());
assert!(!esc.is_graphic());
const fn is_whitespace(self: Self) -> bool

Checks if the value is a whitespace character: 0x20 SPACE, 0x09 HORIZONTAL TAB, 0x0A LINE FEED, 0x0C FORM FEED, or 0x0D CARRIAGE RETURN.

Rust uses the WhatWG Infra Standard's definition of ASCII whitespace. There are several other definitions in wide use. For instance, the POSIX locale includes 0x0B VERTICAL TAB as well as all the above characters, but—from the very same specification—the default rule for "field splitting" in the Bourne shell considers only SPACE, HORIZONTAL TAB, and LINE FEED as whitespace.

If you are writing a program that will process an existing file format, check what that format's definition of whitespace is before using this function.

Examples

#![feature(ascii_char, ascii_char_variants)]
use std::ascii;

let uppercase_a = ascii::Char::CapitalA;
let uppercase_g = ascii::Char::CapitalG;
let a = ascii::Char::SmallA;
let g = ascii::Char::SmallG;
let zero = ascii::Char::Digit0;
let percent = ascii::Char::PercentSign;
let space = ascii::Char::Space;
let lf = ascii::Char::LineFeed;
let esc = ascii::Char::Escape;

assert!(!uppercase_a.is_whitespace());
assert!(!uppercase_g.is_whitespace());
assert!(!a.is_whitespace());
assert!(!g.is_whitespace());
assert!(!zero.is_whitespace());
assert!(!percent.is_whitespace());
assert!(space.is_whitespace());
assert!(lf.is_whitespace());
assert!(!esc.is_whitespace());
const fn is_control(self: Self) -> bool

Checks if the value is a control character: 0x00 NUL ..= 0x1F UNIT SEPARATOR, or 0x7F DELETE. Note that most whitespace characters are control characters, but SPACE is not.

Examples

#![feature(ascii_char, ascii_char_variants)]
use std::ascii;

let uppercase_a = ascii::Char::CapitalA;
let uppercase_g = ascii::Char::CapitalG;
let a = ascii::Char::SmallA;
let g = ascii::Char::SmallG;
let zero = ascii::Char::Digit0;
let percent = ascii::Char::PercentSign;
let space = ascii::Char::Space;
let lf = ascii::Char::LineFeed;
let esc = ascii::Char::Escape;

assert!(!uppercase_a.is_control());
assert!(!uppercase_g.is_control());
assert!(!a.is_control());
assert!(!g.is_control());
assert!(!zero.is_control());
assert!(!percent.is_control());
assert!(!space.is_control());
assert!(lf.is_control());
assert!(esc.is_control());
fn escape_ascii(self: Self) -> EscapeDefault

Returns an iterator that produces an escaped version of a character.

The behavior is identical to ascii::escape_default.

Examples

#![feature(ascii_char, ascii_char_variants)]
use std::ascii;

let zero = ascii::Char::Digit0;
let tab = ascii::Char::CharacterTabulation;
let cr = ascii::Char::CarriageReturn;
let lf = ascii::Char::LineFeed;
let apostrophe = ascii::Char::Apostrophe;
let double_quote = ascii::Char::QuotationMark;
let backslash = ascii::Char::ReverseSolidus;

assert_eq!("0", zero.escape_ascii().to_string());
assert_eq!("\\t", tab.escape_ascii().to_string());
assert_eq!("\\r", cr.escape_ascii().to_string());
assert_eq!("\\n", lf.escape_ascii().to_string());
assert_eq!("\\'", apostrophe.escape_ascii().to_string());
assert_eq!("\\\"", double_quote.escape_ascii().to_string());
assert_eq!("\\\\", backslash.escape_ascii().to_string());

impl Clone for AsciiChar

fn clone(self: &Self) -> AsciiChar

impl Copy for AsciiChar

impl Debug for AsciiChar

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl Default for Char

fn default() -> AsciiChar

Returns the default value of Null

impl Display for AsciiChar

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl Eq for AsciiChar

impl Freeze for AsciiChar

impl Hash for AsciiChar

fn hash<__H: $crate::hash::Hasher>(self: &Self, state: &mut __H)

impl Ord for AsciiChar

fn cmp(self: &Self, other: &AsciiChar) -> Ordering

impl PartialEq for AsciiChar

fn eq(self: &Self, other: &AsciiChar) -> bool

impl PartialOrd for AsciiChar

fn partial_cmp(self: &Self, other: &AsciiChar) -> Option<Ordering>

impl RefUnwindSafe for AsciiChar

impl Send for AsciiChar

impl Step for Char

fn steps_between(start: &AsciiChar, end: &AsciiChar) -> (usize, Option<usize>)
fn forward_checked(start: AsciiChar, count: usize) -> Option<AsciiChar>
fn backward_checked(start: AsciiChar, count: usize) -> Option<AsciiChar>
unsafe fn forward_unchecked(start: AsciiChar, count: usize) -> AsciiChar
unsafe fn backward_unchecked(start: AsciiChar, count: usize) -> AsciiChar

impl StructuralPartialEq for AsciiChar

impl Sync for AsciiChar

impl TrustedStep for Char

impl Unpin for AsciiChar

impl UnsafeUnpin for AsciiChar

impl UnwindSafe for AsciiChar

impl<T> Any for AsciiChar

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for AsciiChar

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for AsciiChar

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for AsciiChar

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for AsciiChar

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into for AsciiChar

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for AsciiChar

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for AsciiChar

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>