Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions library/core/src/char/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not 100% convinced this is needed, but it might be useful to show off that this is the inverse of char::from_u32 because the number looks completely arbitrary on its own.

assert_eq!(char::from_u32(heart.to_u32()), Some(heart));

(Possibly the round-tripping deserves a separate example with its own introduction.)

@okaneco okaneco Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt like this needed an example but wasn't sure exactly what to write. I can note this in the tracking issue as an area for improvement before stabilization.

/// ```
#[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
Expand Down
Loading