Enum AsciiChar
#[repr(u8)]
pub 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 = 0 U+0000 (The default variant)
-
StartOfHeading = 1 U+0001
-
StartOfText = 2 U+0002
-
EndOfText = 3 U+0003
-
EndOfTransmission = 4 U+0004
-
Enquiry = 5 U+0005
-
Acknowledge = 6 U+0006
-
Bell = 7 U+0007
-
Backspace = 8 U+0008
-
CharacterTabulation = 9 U+0009
-
LineFeed = 10 U+000A
-
LineTabulation = 11 U+000B
-
FormFeed = 12 U+000C
-
CarriageReturn = 13 U+000D
-
ShiftOut = 14 U+000E
-
ShiftIn = 15 U+000F
-
DataLinkEscape = 16 U+0010
-
DeviceControlOne = 17 U+0011
-
DeviceControlTwo = 18 U+0012
-
DeviceControlThree = 19 U+0013
-
DeviceControlFour = 20 U+0014
-
NegativeAcknowledge = 21 U+0015
-
SynchronousIdle = 22 U+0016
-
EndOfTransmissionBlock = 23 U+0017
-
Cancel = 24 U+0018
-
EndOfMedium = 25 U+0019
-
Substitute = 26 U+001A
-
Escape = 27 U+001B
-
InformationSeparatorFour = 28 U+001C
-
InformationSeparatorThree = 29 U+001D
-
InformationSeparatorTwo = 30 U+001E
-
InformationSeparatorOne = 31 U+001F
-
Space = 32 U+0020
-
ExclamationMark = 33 U+0021
-
QuotationMark = 34 U+0022
-
NumberSign = 35 U+0023
-
DollarSign = 36 U+0024
-
PercentSign = 37 U+0025
-
Ampersand = 38 U+0026
-
Apostrophe = 39 U+0027
-
LeftParenthesis = 40 U+0028
-
RightParenthesis = 41 U+0029
-
Asterisk = 42 U+002A
-
PlusSign = 43 U+002B
-
Comma = 44 U+002C
-
HyphenMinus = 45 U+002D
-
FullStop = 46 U+002E
-
Solidus = 47 U+002F
-
Digit0 = 48 U+0030
-
Digit1 = 49 U+0031
-
Digit2 = 50 U+0032
-
Digit3 = 51 U+0033
-
Digit4 = 52 U+0034
-
Digit5 = 53 U+0035
-
Digit6 = 54 U+0036
-
Digit7 = 55 U+0037
-
Digit8 = 56 U+0038
-
Digit9 = 57 U+0039
-
Colon = 58 U+003A
-
Semicolon = 59 U+003B
-
LessThanSign = 60 U+003C
-
EqualsSign = 61 U+003D
-
GreaterThanSign = 62 U+003E
-
QuestionMark = 63 U+003F
-
CommercialAt = 64 U+0040
-
CapitalA = 65 U+0041
-
CapitalB = 66 U+0042
-
CapitalC = 67 U+0043
-
CapitalD = 68 U+0044
-
CapitalE = 69 U+0045
-
CapitalF = 70 U+0046
-
CapitalG = 71 U+0047
-
CapitalH = 72 U+0048
-
CapitalI = 73 U+0049
-
CapitalJ = 74 U+004A
-
CapitalK = 75 U+004B
-
CapitalL = 76 U+004C
-
CapitalM = 77 U+004D
-
CapitalN = 78 U+004E
-
CapitalO = 79 U+004F
-
CapitalP = 80 U+0050
-
CapitalQ = 81 U+0051
-
CapitalR = 82 U+0052
-
CapitalS = 83 U+0053
-
CapitalT = 84 U+0054
-
CapitalU = 85 U+0055
-
CapitalV = 86 U+0056
-
CapitalW = 87 U+0057
-
CapitalX = 88 U+0058
-
CapitalY = 89 U+0059
-
CapitalZ = 90 U+005A
-
LeftSquareBracket = 91 U+005B
-
ReverseSolidus = 92 U+005C
-
RightSquareBracket = 93 U+005D
-
CircumflexAccent = 94 U+005E
-
LowLine = 95 U+005F
-
GraveAccent = 96 U+0060
-
SmallA = 97 U+0061
-
SmallB = 98 U+0062
-
SmallC = 99 U+0063
-
SmallD = 100 U+0064
-
SmallE = 101 U+0065
-
SmallF = 102 U+0066
-
SmallG = 103 U+0067
-
SmallH = 104 U+0068
-
SmallI = 105 U+0069
-
SmallJ = 106 U+006A
-
SmallK = 107 U+006B
-
SmallL = 108 U+006C
-
SmallM = 109 U+006D
-
SmallN = 110 U+006E
-
SmallO = 111 U+006F
-
SmallP = 112 U+0070
-
SmallQ = 113 U+0071
-
SmallR = 114 U+0072
-
SmallS = 115 U+0073
-
SmallT = 116 U+0074
-
SmallU = 117 U+0075
-
SmallV = 118 U+0076
-
SmallW = 119 U+0077
-
SmallX = 120 U+0078
-
SmallY = 121 U+0079
-
SmallZ = 122 U+007A
-
LeftCurlyBracket = 123 U+007B
-
VerticalLine = 124 U+007C
-
RightCurlyBracket = 125 U+007D
-
Tilde = 126 U+007E
-
Delete = 127 U+007F
Implementations
impl AsciiChar
const MIN: Self = Self::Null;The character with the lowest ASCII code.
const MAX: Self = Self::Delete;The character with the highest ASCII code.
const fn from_u8(b: u8) -> Option<Self>Creates an ASCII character from the byte
b, or returnsNoneif it's too large.const unsafe 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.const unsafe 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) -> u8Gets this ASCII character as a byte.
const fn to_char(self) -> charGets this ASCII character as a
charUnicode Scalar Value.const fn as_str(&self) -> &strViews this ASCII character as a one-code-unit UTF-8
str.const fn to_uppercase(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) -> 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, 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(&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(&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) -> 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) -> 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) -> 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) -> 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) -> 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) -> 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) -> 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) -> boolChecks if the value is a punctuation or symbol character (i.e. not alphanumeric, whitespace, or control):
- 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) -> boolChecks if the value is a graphic character (i.e. not whitespace or control): 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) -> 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) -> 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) -> 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!;
Trait Implementations
impl BytewiseEq for Char
impl Clone for AsciiChar
fn clone(&self) -> AsciiChar
impl Copy for AsciiChar
impl Debug for AsciiChar
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Default for Char
fn default() -> AsciiCharReturns the default value of
Null
impl Display for AsciiChar
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Eq for AsciiChar
fn assert_fields_are_eq(&self)
impl Hash for AsciiChar
fn hash<__H: Hasher>(&self, state: &mut __H)
impl Ord for AsciiChar
fn cmp(&self, other: &AsciiChar) -> Ordering
impl PartialEq for AsciiChar
fn eq(&self, other: &AsciiChar) -> bool
impl PartialOrd for AsciiChar
fn partial_cmp(&self, other: &AsciiChar) -> Option<Ordering>
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>fn forward_overflowing(start: Self, count: usize) -> (Self, bool)fn backward_overflowing(start: Self, count: usize) -> (Self, bool)unsafe fn forward_unchecked(start: AsciiChar, count: usize) -> AsciiCharunsafe fn backward_unchecked(start: AsciiChar, count: usize) -> AsciiChar
impl StructuralPartialEq for AsciiChar
impl TrivialClone for AsciiChar
impl TrustedStep for Char
impl UnsignedBytewiseOrd for Char
Auto Trait Implementations
impl Freeze for AsciiChar
impl RefUnwindSafe for AsciiChar
impl Send for AsciiChar
impl Sync for AsciiChar
impl Unpin for AsciiChar
impl UnsafeUnpin for AsciiChar
impl UnwindSafe for AsciiChar
Blanket Implementations
impl<T> Any for AsciiChar
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for AsciiChar
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for AsciiChar
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for AsciiChar
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for AsciiChar
fn from(t: T) -> TReturns the argument unchanged.
impl<T> Printable for AsciiChar
where
T: Copy + Debug,
impl<T> SizeHint for AsciiChar
where
T: ?Sized,
fn lower_bound(&self) -> usizefn upper_bound(&self) -> Option<usize>
impl<T> SizedTypeProperties for AsciiChar
impl<T, U> Into<U> for AsciiChar
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for AsciiChar
where
U: Into<T>,
type Error = Infallible;fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto<U> for AsciiChar
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>