Auto merge of #114299 - clarfonthey:char-min, r=dtolnay,BurntSushi

Add char::MIN

ACP: rust-lang/libs-team#252
Tracking issue: #114298

r? `@rust-lang/libs-api`
This commit is contained in:
bors 2023-09-08 00:02:48 +00:00
commit feb06732c0

View File

@ -9,8 +9,58 @@ use crate::unicode::{self, conversions};
use super::*;
impl char {
/// The lowest valid code point a `char` can have, `'\0'`.
///
/// Unlike integer types, `char` actually has a gap in the middle,
/// meaning that the range of possible `char`s is smaller than you
/// might expect. Ranges of `char` will automatically hop this gap
/// for you:
///
/// ```
/// #![feature(char_min)]
/// let dist = u32::from(char::MAX) - u32::from(char::MIN);
/// let size = (char::MIN..=char::MAX).count() as u32;
/// assert!(size < dist);
/// ```
///
/// Despite this gap, the `MIN` and [`MAX`] values can be used as bounds for
/// all `char` values.
///
/// [`MAX`]: char::MAX
///
/// # Examples
///
/// ```
/// #![feature(char_min)]
/// # fn something_which_returns_char() -> char { 'a' }
/// let c: char = something_which_returns_char();
/// assert!(char::MIN <= c);
///
/// let value_at_min = u32::from(char::MIN);
/// assert_eq!(char::from_u32(value_at_min), Some('\0'));
/// ```
#[unstable(feature = "char_min", issue = "114298")]
pub const MIN: char = '\0';
/// The highest valid code point a `char` can have, `'\u{10FFFF}'`.
///
/// Unlike integer types, `char` actually has a gap in the middle,
/// meaning that the range of possible `char`s is smaller than you
/// might expect. Ranges of `char` will automatically hop this gap
/// for you:
///
/// ```
/// #![feature(char_min)]
/// let dist = u32::from(char::MAX) - u32::from(char::MIN);
/// let size = (char::MIN..=char::MAX).count() as u32;
/// assert!(size < dist);
/// ```
///
/// Despite this gap, the [`MIN`] and `MAX` values can be used as bounds for
/// all `char` values.
///
/// [`MIN`]: char::MIN
///
/// # Examples
///
/// ```
@ -18,7 +68,7 @@ impl char {
/// let c: char = something_which_returns_char();
/// assert!(c <= char::MAX);
///
/// let value_at_max = char::MAX as u32;
/// let value_at_max = u32::from(char::MAX);
/// assert_eq!(char::from_u32(value_at_max), Some('\u{10FFFF}'));
/// assert_eq!(char::from_u32(value_at_max + 1), None);
/// ```