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:
- For
<control>characters, the primary alias name is used. LATINis dropped, as this block has no non-latin letters.LETTERis dropped, asCAPITAL/SMALLsuffices in this block.DIGITs use a single digit rather than writing outZERO,ONE, etc.
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 returnsNoneif it's too large.unsafe const fn from_u8_unchecked(b: u8) -> SelfCreates an ASCII character from the byte
b, without checking whether it's valid.Safety
bmust be in0..=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, returnsNone.unsafe const fn digit_unchecked(d: u8) -> SelfWhen 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 >= 10andd <= 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) -> u8Gets this ASCII character as a byte.
const fn to_char(self: Self) -> charGets this ASCII character as a
charUnicode Scalar Value.const fn as_str(self: &Self) -> &strViews this ASCII character as a one-code-unit UTF-8
str.const fn to_uppercase(self: Self) -> SelfMakes 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
use ascii; let lowercase_a = SmallA; assert_eq!;const fn to_lowercase(self: Self) -> SelfMakes 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
use ascii; let uppercase_a = CapitalA; assert_eq!;const fn eq_ignore_case(self: Self, other: Self) -> boolChecks that two values are a case-insensitive match.
This is equivalent to
to_lowercase(a) == to_lowercase(b).Examples
use ascii; let lowercase_a = SmallA; let uppercase_a = CapitalA; assert!;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
use ascii; let mut letter_a = SmallA; letter_a.make_uppercase; assert_eq!;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
use ascii; let mut letter_a = CapitalA; letter_a.make_lowercase; assert_eq!;const fn is_alphabetic(self: Self) -> boolChecks if the value is an alphabetic character:
- 0x41 'A' ..= 0x5A 'Z', or
- 0x61 'a' ..= 0x7A 'z'.
Examples
use ascii; let uppercase_a = CapitalA; let uppercase_g = CapitalG; let a = SmallA; let g = SmallG; let zero = Digit0; let percent = PercentSign; let space = Space; let lf = LineFeed; let esc = Escape; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!;const fn is_uppercase(self: Self) -> boolChecks if the value is an uppercase character: 0x41 'A' ..= 0x5A 'Z'.
Examples
use ascii; let uppercase_a = CapitalA; let uppercase_g = CapitalG; let a = SmallA; let g = SmallG; let zero = Digit0; let percent = PercentSign; let space = Space; let lf = LineFeed; let esc = Escape; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!;const fn is_lowercase(self: Self) -> boolChecks if the value is a lowercase character: 0x61 'a' ..= 0x7A 'z'.
Examples
use ascii; let uppercase_a = CapitalA; let uppercase_g = CapitalG; let a = SmallA; let g = SmallG; let zero = Digit0; let percent = PercentSign; let space = Space; let lf = LineFeed; let esc = Escape; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!;const fn is_alphanumeric(self: Self) -> boolChecks if the value is an alphanumeric character:
- 0x41 'A' ..= 0x5A 'Z', or
- 0x61 'a' ..= 0x7A 'z', or
- 0x30 '0' ..= 0x39 '9'.
Examples
use ascii; let uppercase_a = CapitalA; let uppercase_g = CapitalG; let a = SmallA; let g = SmallG; let zero = Digit0; let percent = PercentSign; let space = Space; let lf = LineFeed; let esc = Escape; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!;const fn is_digit(self: Self) -> boolChecks if the value is a decimal digit: 0x30 '0' ..= 0x39 '9'.
Examples
use ascii; let uppercase_a = CapitalA; let uppercase_g = CapitalG; let a = SmallA; let g = SmallG; let zero = Digit0; let percent = PercentSign; let space = Space; let lf = LineFeed; let esc = Escape; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!;const fn is_octdigit(self: Self) -> boolChecks if the value is an octal digit: 0x30 '0' ..= 0x37 '7'.
Examples
use ascii; let uppercase_a = CapitalA; let a = SmallA; let zero = Digit0; let seven = Digit7; let eight = Digit8; let percent = PercentSign; let lf = LineFeed; let esc = Escape; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!;const fn is_hexdigit(self: Self) -> boolChecks if the value is a hexadecimal digit:
- 0x30 '0' ..= 0x39 '9', or
- 0x41 'A' ..= 0x46 'F', or
- 0x61 'a' ..= 0x66 'f'.
Examples
use ascii; let uppercase_a = CapitalA; let uppercase_g = CapitalG; let a = SmallA; let g = SmallG; let zero = Digit0; let percent = PercentSign; let space = Space; let lf = LineFeed; let esc = Escape; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!;const fn is_punctuation(self: Self) -> boolChecks if the value is a punctuation character:
- 0x21 ..= 0x2F
! " # $ % & ' ( ) * + , - . /, or - 0x3A ..= 0x40
: ; < = > ? @, or - 0x5B ..= 0x60
[ \ ] ^ _ `, or - 0x7B ..= 0x7E
{ | } ~
Examples
use ascii; let uppercase_a = CapitalA; let uppercase_g = CapitalG; let a = SmallA; let g = SmallG; let zero = Digit0; let percent = PercentSign; let space = Space; let lf = LineFeed; let esc = Escape; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!;- 0x21 ..= 0x2F
const fn is_graphic(self: Self) -> boolChecks if the value is a graphic character: 0x21 '!' ..= 0x7E '~'.
Examples
use ascii; let uppercase_a = CapitalA; let uppercase_g = CapitalG; let a = SmallA; let g = SmallG; let zero = Digit0; let percent = PercentSign; let space = Space; let lf = LineFeed; let esc = Escape; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!;const fn is_whitespace(self: Self) -> boolChecks 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
use ascii; let uppercase_a = CapitalA; let uppercase_g = CapitalG; let a = SmallA; let g = SmallG; let zero = Digit0; let percent = PercentSign; let space = Space; let lf = LineFeed; let esc = Escape; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!;const fn is_control(self: Self) -> boolChecks 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
use ascii; let uppercase_a = CapitalA; let uppercase_g = CapitalG; let a = SmallA; let g = SmallG; let zero = Digit0; let percent = PercentSign; let space = Space; let lf = LineFeed; let esc = Escape; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!; assert!;fn escape_ascii(self: Self) -> EscapeDefaultReturns an iterator that produces an escaped version of a character.
The behavior is identical to
ascii::escape_default.Examples
use ascii; let zero = Digit0; let tab = CharacterTabulation; let cr = CarriageReturn; let lf = LineFeed; let apostrophe = Apostrophe; let double_quote = QuotationMark; let backslash = ReverseSolidus; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;
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() -> AsciiCharReturns 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) -> AsciiCharunsafe 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) -> TReturns the argument unchanged.
impl<T, U> Into for AsciiChar
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses 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>