Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add char::code_point #83496

Closed
wants to merge 4 commits into from
Closed
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
35 changes: 35 additions & 0 deletions library/core/src/char/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,41 @@ impl char {
encode_utf16_raw(self as u32, dst)
}

/// Returns the ["Unicode code point"][codepoint] associated with this `char`.
///
/// Although ["Unicode scalar values"][scalar] and ["code points"][codepoint] are different,
/// all scalar values are code points. Because a `char` is a scalar value, it therefore is also
/// a valid code point. This method provides that code point as a `u32`.
///
/// [scalar]: http://www.unicode.org/glossary/#unicode_scalar_value
/// [codepoint]: http://www.unicode.org/glossary/#code_point
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// assert_eq!('💯'.code_point(), 0x1F4AF);
/// assert_eq!('❤'.code_point(), 0x2764);
/// assert_eq!('a'.code_point(), 97);
/// ```
///
/// This method is equivalent to casting via `as`, but with a more descriptive name:
///
/// ```
/// assert_eq!('a'.code_point(), 'a' as u32);
/// assert_eq!('ß'.code_point(), 'ß' as u32);
/// assert_eq!('\n'.code_point(), '\n' as u32);
/// ```
#[stable(feature = "char_code_point", since = "1.53.0")]
#[rustc_const_stable(feature = "char_code_point", since = "1.53.0")]
#[inline]
pub const fn code_point(self) -> u32 {
// Casting a `char` to a `u32` gives the underlying scalar value, and all scalar values are
// valid code points
self as u32
}

/// Returns `true` if this `char` has the `Alphabetic` property.
///
/// `Alphabetic` is described in Chapter 4 (Character Properties) of the [Unicode Standard] and
Expand Down