#[repr(u8)]
pub enum AsciiChar {
Show 128 variants
Null,
SOH,
SOX,
ETX,
EOT,
ENQ,
ACK,
Bell,
BackSpace,
Tab,
LineFeed,
VT,
FF,
CarriageReturn,
SI,
SO,
DLE,
DC1,
DC2,
DC3,
DC4,
NAK,
SYN,
ETB,
CAN,
EM,
SUB,
ESC,
FS,
GS,
RS,
US,
Space,
Exclamation,
Quotation,
Hash,
Dollar,
Percent,
Ampersand,
Apostrophe,
ParenOpen,
ParenClose,
Asterisk,
Plus,
Comma,
Minus,
Dot,
Slash,
_0,
_1,
_2,
_3,
_4,
_5,
_6,
_7,
_8,
_9,
Colon,
Semicolon,
LessThan,
Equal,
GreaterThan,
Question,
At,
A,
B,
C,
D,
E,
F,
G,
H,
I,
J,
K,
L,
M,
N,
O,
P,
Q,
R,
S,
T,
U,
V,
W,
X,
Y,
Z,
BracketOpen,
BackSlash,
BracketClose,
Caret,
UnderScore,
Grave,
a,
b,
c,
d,
e,
f,
g,
h,
i,
j,
k,
l,
m,
n,
o,
p,
q,
r,
s,
t,
u,
v,
w,
x,
y,
z,
CurlyBraceOpen,
VerticalBar,
CurlyBraceClose,
Tilde,
DEL,
}
Expand description
An ASCII character. It wraps a u8
, with the highest bit always zero.
Variants
Null
'\0'
SOH
SOX
ETX
EOT
ENQ
ACK
Bell
'\a'
is not recognized by Rust.
BackSpace
'\b'
is not recognized by Rust.
Tab
'\t'
LineFeed
'\n'
VT
'\v'
is not recognized by Rust.
FF
'\f'
is not recognized by Rust.
CarriageReturn
'\r'
SI
SO
DLE
DC1
DC2
Device control 2
DC3
Device control 3, Often XOFF
DC4
Device control 4
NAK
SYN
ETB
CAN
EM
SUB
ESC
'\e'
is not recognized by Rust.
FS
GS
RS
US
Space
' '
Exclamation
'!'
Quotation
'"'
Hash
'#'
Dollar
'$'
Percent
'%'
Ampersand
'&'
Apostrophe
'\''
ParenOpen
'('
ParenClose
')'
Asterisk
'*'
Plus
'+'
Comma
','
Minus
'-'
Dot
'.'
Slash
'/'
_0
'0'
_1
'1'
_2
'2'
_3
'3'
_4
'4'
_5
'5'
_6
'6'
_7
'7'
_8
'8'
_9
'9'
Colon
':'
Semicolon
';'
LessThan
'<'
Equal
'='
GreaterThan
'>'
Question
'?'
At
'@'
A
'A'
B
'B'
C
'C'
D
'D'
E
'E'
F
'F'
G
'G'
H
'H'
I
'I'
J
'J'
K
'K'
L
'L'
M
'M'
N
'N'
O
'O'
P
'P'
Q
'Q'
R
'R'
S
'S'
T
'T'
U
'U'
V
'V'
W
'W'
X
'X'
Y
'Y'
Z
'Z'
BracketOpen
'['
BackSlash
'\'
BracketClose
']'
Caret
'^'
UnderScore
'_'
Grave
'
’`
a
'a'
b
'b'
c
'c'
d
'd'
e
'e'
f
'f'
g
'g'
h
'h'
i
'i'
j
'j'
k
'k'
l
'l'
m
'm'
n
'n'
o
'o'
p
'p'
q
'q'
r
'r'
s
's'
t
't'
u
'u'
v
'v'
w
'w'
x
'x'
y
'y'
z
'z'
CurlyBraceOpen
'{'
VerticalBar
'|'
CurlyBraceClose
'}'
Tilde
'~'
DEL
Implementations
sourceimpl AsciiChar
impl AsciiChar
sourcepub fn from_ascii<C: ToAsciiChar>(ch: C) -> Result<Self, ToAsciiCharError>
pub fn from_ascii<C: ToAsciiChar>(ch: C) -> Result<Self, ToAsciiCharError>
sourcepub const fn new(ch: char) -> AsciiChar
pub const fn new(ch: char) -> AsciiChar
Create an AsciiChar
from a char
, panicking if it’s not ASCII.
This function is intended for creating AsciiChar
values from
hardcoded known-good character literals such as 'K'
, '-'
or '\0'
,
and for use in const
contexts.
Use from_ascii()
instead when you’re not
certain the character is ASCII.
Examples
assert_eq!(AsciiChar::new('@'), AsciiChar::At);
assert_eq!(AsciiChar::new('C').as_char(), 'C');
In a constant:
const SPLIT_ON: AsciiChar = AsciiChar::new(',');
This will not compile:
const BAD: AsciiChar = AsciiChar::new('Ø');
Panics
This function will panic if passed a non-ASCII character.
The panic message might not be the most descriptive due to the
current limitations of const fn
.
sourcepub unsafe fn from_ascii_unchecked(ch: u8) -> Self
pub unsafe fn from_ascii_unchecked(ch: u8) -> Self
Constructs an ASCII character from a u8
, char
or other character
type without any checks.
Safety
This function is very unsafe as it can create invalid enum
discriminants, which instantly creates undefined behavior.
(let _ = AsciiChar::from_ascii_unchecked(200);
alone is UB).
The undefined behavior is not just theoretical either:
For example, [0; 128][AsciiChar::from_ascii_unchecked(255) as u8 as usize] = 0
might not panic, creating a buffer overflow,
and Some(AsciiChar::from_ascii_unchecked(128))
might be None
.
sourcepub const fn is_alphabetic(self) -> bool
pub const fn is_alphabetic(self) -> bool
Check if the character is a letter (a-z, A-Z)
sourcepub const fn is_ascii_alphabetic(&self) -> bool
pub const fn is_ascii_alphabetic(&self) -> bool
Check if the character is a letter (a-z, A-Z).
This method is identical to is_alphabetic()
sourcepub fn is_digit(self, radix: u32) -> bool
pub fn is_digit(self, radix: u32) -> bool
Check if the character is a digit in the given radix.
If the radix is always 10 or 16,
is_ascii_digit()
and
is_ascii_hexdigit()
will be faster.
Panics
Radixes greater than 36 are not supported and will result in a panic.
sourcepub const fn is_ascii_digit(&self) -> bool
pub const fn is_ascii_digit(&self) -> bool
Check if the character is a number (0-9)
Examples
assert_eq!(AsciiChar::new('0').is_ascii_digit(), true);
assert_eq!(AsciiChar::new('9').is_ascii_digit(), true);
assert_eq!(AsciiChar::new('a').is_ascii_digit(), false);
assert_eq!(AsciiChar::new('A').is_ascii_digit(), false);
assert_eq!(AsciiChar::new('/').is_ascii_digit(), false);
sourcepub const fn is_alphanumeric(self) -> bool
pub const fn is_alphanumeric(self) -> bool
Check if the character is a letter or number
sourcepub const fn is_ascii_alphanumeric(&self) -> bool
pub const fn is_ascii_alphanumeric(&self) -> bool
Check if the character is a letter or number
This method is identical to is_alphanumeric()
sourcepub const fn is_ascii_blank(&self) -> bool
pub const fn is_ascii_blank(&self) -> bool
Check if the character is a space or horizontal tab
Examples
assert!(AsciiChar::Space.is_ascii_blank());
assert!(AsciiChar::Tab.is_ascii_blank());
assert!(!AsciiChar::VT.is_ascii_blank());
assert!(!AsciiChar::LineFeed.is_ascii_blank());
assert!(!AsciiChar::CarriageReturn.is_ascii_blank());
assert!(!AsciiChar::FF.is_ascii_blank());
sourcepub const fn is_whitespace(self) -> bool
pub const fn is_whitespace(self) -> bool
Check if the character one of ’ ’, ‘\t’, ‘\n’, ‘\r’, ‘\0xb’ (vertical tab) or ‘\0xc’ (form feed).
sourcepub const fn is_ascii_whitespace(&self) -> bool
pub const fn is_ascii_whitespace(&self) -> bool
Check if the character is a ’ ’, ‘\t’, ‘\n’, ‘\r’ or ‘\0xc’ (form feed).
This method is NOT identical to is_whitespace()
.
sourcepub const fn is_ascii_control(&self) -> bool
pub const fn is_ascii_control(&self) -> bool
Check if the character is a control character
Examples
assert_eq!(AsciiChar::new('\0').is_ascii_control(), true);
assert_eq!(AsciiChar::new('n').is_ascii_control(), false);
assert_eq!(AsciiChar::new(' ').is_ascii_control(), false);
assert_eq!(AsciiChar::new('\n').is_ascii_control(), true);
assert_eq!(AsciiChar::new('\t').is_ascii_control(), true);
assert_eq!(AsciiChar::EOT.is_ascii_control(), true);
sourcepub const fn is_ascii_graphic(&self) -> bool
pub const fn is_ascii_graphic(&self) -> bool
Checks if the character is printable (except space)
Examples
assert_eq!(AsciiChar::new('n').is_ascii_graphic(), true);
assert_eq!(AsciiChar::new(' ').is_ascii_graphic(), false);
assert_eq!(AsciiChar::new('\n').is_ascii_graphic(), false);
sourcepub const fn is_ascii_printable(&self) -> bool
pub const fn is_ascii_printable(&self) -> bool
Checks if the character is printable (including space)
Examples
assert_eq!(AsciiChar::new('n').is_ascii_printable(), true);
assert_eq!(AsciiChar::new(' ').is_ascii_printable(), true);
assert_eq!(AsciiChar::new('\n').is_ascii_printable(), false);
sourcepub const fn is_lowercase(self) -> bool
pub const fn is_lowercase(self) -> bool
Checks if the character is alphabetic and lowercase (a-z).
Examples
use ascii::AsciiChar;
assert_eq!(AsciiChar::new('a').is_lowercase(), true);
assert_eq!(AsciiChar::new('A').is_lowercase(), false);
assert_eq!(AsciiChar::new('@').is_lowercase(), false);
sourcepub const fn is_ascii_lowercase(&self) -> bool
pub const fn is_ascii_lowercase(&self) -> bool
Checks if the character is alphabetic and lowercase (a-z).
This method is identical to is_lowercase()
sourcepub const fn is_uppercase(self) -> bool
pub const fn is_uppercase(self) -> bool
Checks if the character is alphabetic and uppercase (A-Z).
Examples
assert_eq!(AsciiChar::new('A').is_uppercase(), true);
assert_eq!(AsciiChar::new('a').is_uppercase(), false);
assert_eq!(AsciiChar::new('@').is_uppercase(), false);
sourcepub const fn is_ascii_uppercase(&self) -> bool
pub const fn is_ascii_uppercase(&self) -> bool
Checks if the character is alphabetic and uppercase (A-Z).
This method is identical to is_uppercase()
sourcepub const fn is_ascii_punctuation(&self) -> bool
pub const fn is_ascii_punctuation(&self) -> bool
Checks if the character is punctuation
Examples
assert_eq!(AsciiChar::new('n').is_ascii_punctuation(), false);
assert_eq!(AsciiChar::new(' ').is_ascii_punctuation(), false);
assert_eq!(AsciiChar::new('_').is_ascii_punctuation(), true);
assert_eq!(AsciiChar::new('~').is_ascii_punctuation(), true);
sourcepub const fn is_ascii_hexdigit(&self) -> bool
pub const fn is_ascii_hexdigit(&self) -> bool
Checks if the character is a valid hex digit
Examples
assert_eq!(AsciiChar::new('5').is_ascii_hexdigit(), true);
assert_eq!(AsciiChar::new('a').is_ascii_hexdigit(), true);
assert_eq!(AsciiChar::new('F').is_ascii_hexdigit(), true);
assert_eq!(AsciiChar::new('G').is_ascii_hexdigit(), false);
assert_eq!(AsciiChar::new(' ').is_ascii_hexdigit(), false);
sourcepub fn as_printable_char(self) -> char
pub fn as_printable_char(self) -> char
Unicode has printable versions of the ASCII control codes, like ‘␛’.
This function is identical with .as_char()
for all values .is_printable()
returns true for,
but replaces the control codes with those unicodes printable versions.
Examples
assert_eq!(AsciiChar::new('\0').as_printable_char(), '␀');
assert_eq!(AsciiChar::new('\n').as_printable_char(), '␊');
assert_eq!(AsciiChar::new(' ').as_printable_char(), ' ');
assert_eq!(AsciiChar::new('p').as_printable_char(), 'p');
sourcepub fn make_ascii_uppercase(&mut self)
pub fn make_ascii_uppercase(&mut self)
Replaces letters a
to z
with A
to Z
sourcepub fn make_ascii_lowercase(&mut self)
pub fn make_ascii_lowercase(&mut self)
Replaces letters A
to Z
with a
to z
sourcepub const fn to_ascii_uppercase(&self) -> Self
pub const fn to_ascii_uppercase(&self) -> Self
Maps letters a-z to A-Z and returns any other character unchanged.
Examples
assert_eq!(AsciiChar::new('u').to_ascii_uppercase().as_char(), 'U');
assert_eq!(AsciiChar::new('U').to_ascii_uppercase().as_char(), 'U');
assert_eq!(AsciiChar::new('2').to_ascii_uppercase().as_char(), '2');
assert_eq!(AsciiChar::new('=').to_ascii_uppercase().as_char(), '=');
assert_eq!(AsciiChar::new('[').to_ascii_uppercase().as_char(), '[');
sourcepub const fn to_ascii_lowercase(&self) -> Self
pub const fn to_ascii_lowercase(&self) -> Self
Maps letters A-Z to a-z and returns any other character unchanged.
Examples
assert_eq!(AsciiChar::new('U').to_ascii_lowercase().as_char(), 'u');
assert_eq!(AsciiChar::new('u').to_ascii_lowercase().as_char(), 'u');
assert_eq!(AsciiChar::new('2').to_ascii_lowercase().as_char(), '2');
assert_eq!(AsciiChar::new('^').to_ascii_lowercase().as_char(), '^');
assert_eq!(AsciiChar::new('\x7f').to_ascii_lowercase().as_char(), '\x7f');
sourcepub const fn eq_ignore_ascii_case(&self, other: &Self) -> bool
pub const fn eq_ignore_ascii_case(&self, other: &Self) -> bool
Compares two characters case-insensitively.
Trait Implementations
sourceimpl From<AsciiChar> for AsciiString
impl From<AsciiChar> for AsciiString
sourceimpl Ord for AsciiChar
impl Ord for AsciiChar
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
sourceimpl PartialOrd<AsciiChar> for AsciiChar
impl PartialOrd<AsciiChar> for AsciiChar
sourcefn partial_cmp(&self, other: &AsciiChar) -> Option<Ordering>
fn partial_cmp(&self, other: &AsciiChar) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresourceimpl PartialOrd<AsciiChar> for char
impl PartialOrd<AsciiChar> for char
sourcefn partial_cmp(&self, rhs: &AsciiChar) -> Option<Ordering>
fn partial_cmp(&self, rhs: &AsciiChar) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresourceimpl PartialOrd<AsciiChar> for u8
impl PartialOrd<AsciiChar> for u8
sourcefn partial_cmp(&self, rhs: &AsciiChar) -> Option<Ordering>
fn partial_cmp(&self, rhs: &AsciiChar) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresourceimpl PartialOrd<char> for AsciiChar
impl PartialOrd<char> for AsciiChar
sourcefn partial_cmp(&self, rhs: &char) -> Option<Ordering>
fn partial_cmp(&self, rhs: &char) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresourceimpl PartialOrd<u8> for AsciiChar
impl PartialOrd<u8> for AsciiChar
sourcefn partial_cmp(&self, rhs: &u8) -> Option<Ordering>
fn partial_cmp(&self, rhs: &u8) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresourceimpl ToAsciiChar for AsciiChar
impl ToAsciiChar for AsciiChar
sourcefn to_ascii_char(self) -> Result<AsciiChar, ToAsciiCharError>
fn to_ascii_char(self) -> Result<AsciiChar, ToAsciiCharError>
AsciiChar
. Read moresourceunsafe fn to_ascii_char_unchecked(self) -> AsciiChar
unsafe fn to_ascii_char_unchecked(self) -> AsciiChar
AsciiChar
without checking that it is an ASCII character. Read more