From 77d7cf2ce5b2364c2aa8807d7a30e3e4fb5767f5 Mon Sep 17 00:00:00 2001 From: okaneco <47607823+okaneco@users.noreply.github.com> Date: Tue, 28 Apr 2026 05:15:44 -0400 Subject: [PATCH] Implement feature `char_to_u32` Add method `char::to_u32` which returns the underlying value of `char`. --- library/core/src/char/methods.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index d0114c30a6b3c..2af5b4ef46b97 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -1812,6 +1812,28 @@ impl char { ToCasefold(CaseMappingIter::new(conversions::to_casefold(self))) } + /// Returns the code point value as a `u32`. + /// + /// # Examples + /// + /// ``` + /// #![feature(char_to_u32)] + /// + /// let ascii = 'a'; + /// let heart = '❤'; + /// + /// assert_eq!(ascii.to_u32(), 97_u32); + /// assert_eq!(heart.to_u32(), 0x2764_u32); + /// ``` + #[must_use = "this returns the result of the operation, \ + without modifying the original"] + #[unstable(feature = "char_to_u32", issue = "158938")] + #[rustc_const_unstable(feature = "char_to_u32", issue = "158938")] + #[inline(always)] + pub const fn to_u32(self) -> u32 { + self as u32 + } + /// Checks if the value is within the ASCII range. /// /// # Examples