From f65fbe95171033f1d7377f13500de5e13e6f7ca1 Mon Sep 17 00:00:00 2001 From: ltdk Date: Mon, 31 Jul 2023 12:34:55 -0400 Subject: [PATCH 1/5] Add char::MIN --- library/core/src/char/methods.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index 515b8d20ead86..cca32c2dd37d3 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -9,6 +9,21 @@ use crate::unicode::{self, conversions}; use super::*; impl char { + /// The lowest valid code point a `char` can have, `'\0'`. + /// + /// # Examples + /// + /// ``` + /// # fn something_which_returns_char() -> char { 'a' } + /// let c: char = something_which_returns_char(); + /// assert!(char::MIN <= c); + /// + /// let value_at_min = char::MIN as u32; + /// 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}'`. /// /// # Examples From b64f3c7181ccc66aca595c2b95e5a2c64b9525c6 Mon Sep 17 00:00:00 2001 From: ltdk Date: Mon, 31 Jul 2023 13:21:42 -0400 Subject: [PATCH 2/5] Add note on gap for MIN/MAX --- library/core/src/char/methods.rs | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index cca32c2dd37d3..c875f4503be37 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -11,9 +11,27 @@ 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(); + /// assert!(dist < size); + /// ``` + /// + /// 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); @@ -26,6 +44,23 @@ impl char { /// 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(); + /// assert!(dist < size); + /// ``` + /// + /// Despite this gap, the [`MIN`] and `MAX` values can be used as bounds for + /// all `char` values. + /// + /// [`MIN`]: char::MIN + /// /// # Examples /// /// ``` From 0165a4cf5f1b22a51d316e73d28422104b05ce6d Mon Sep 17 00:00:00 2001 From: ltdk Date: Mon, 31 Jul 2023 13:22:16 -0400 Subject: [PATCH 3/5] Use u32::from for MIN/MAX examples --- library/core/src/char/methods.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index c875f4503be37..c5eccf2998f3b 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -36,7 +36,7 @@ impl char { /// let c: char = something_which_returns_char(); /// assert!(char::MIN <= c); /// - /// let value_at_min = char::MIN as u32; + /// let value_at_min = u32::from(char::MIN); /// assert_eq!(char::from_u32(value_at_min), Some('\0')); /// ``` #[unstable(feature = "char_min", issue = "114298")] @@ -68,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); /// ``` From bd6ccf31de722eb75478c418cac8b5ff5338c53d Mon Sep 17 00:00:00 2001 From: ltdk Date: Mon, 31 Jul 2023 13:24:30 -0400 Subject: [PATCH 4/5] Can't compare usize and u32 --- library/core/src/char/methods.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index c5eccf2998f3b..feb8ee2bbda21 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -19,7 +19,7 @@ impl char { /// ``` /// #![feature(char_min)] /// let dist = u32::from(char::MAX) - u32::from(char::MIN); - /// let size = (char::MIN..=char::MAX).count(); + /// let size = (char::MIN..=char::MAX).count() as u32; /// assert!(dist < size); /// ``` /// @@ -52,7 +52,7 @@ impl char { /// ``` /// #![feature(char_min)] /// let dist = u32::from(char::MAX) - u32::from(char::MIN); - /// let size = (char::MIN..=char::MAX).count(); + /// let size = (char::MIN..=char::MAX).count() as u32; /// assert!(dist < size); /// ``` /// From 9fce8abe0bf135d05731dff415e6312472c043b7 Mon Sep 17 00:00:00 2001 From: ltdk Date: Mon, 31 Jul 2023 15:08:52 -0400 Subject: [PATCH 5/5] I'm mathematically challenged --- library/core/src/char/methods.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index feb8ee2bbda21..4ac956e7b76dc 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -20,7 +20,7 @@ impl char { /// #![feature(char_min)] /// let dist = u32::from(char::MAX) - u32::from(char::MIN); /// let size = (char::MIN..=char::MAX).count() as u32; - /// assert!(dist < size); + /// assert!(size < dist); /// ``` /// /// Despite this gap, the `MIN` and [`MAX`] values can be used as bounds for @@ -53,7 +53,7 @@ impl char { /// #![feature(char_min)] /// let dist = u32::from(char::MAX) - u32::from(char::MIN); /// let size = (char::MIN..=char::MAX).count() as u32; - /// assert!(dist < size); + /// assert!(size < dist); /// ``` /// /// Despite this gap, the [`MIN`] and `MAX` values can be used as bounds for