@@ -63,19 +63,63 @@ const MAX_THREE_B: u32 = 0x10000;
6363 Cn Unassigned a reserved unassigned code point or a noncharacter
6464*/
6565
66- /// The highest valid code point
66+ /// The highest valid code point a `char` can have.
67+ ///
68+ /// A [`char`] is a [Unicode Scalar Value], which means that it is a [Code
69+ /// Point], but only ones within a certain range. `MAX` is the highest valid
70+ /// code point that's a valid [Unicode Scalar Value].
71+ ///
72+ /// [`char`]: primitive.char.html
73+ /// [Unicode Scalar Value]: http://www.unicode.org/glossary/#unicode_scalar_value
74+ /// [Code Point]: http://www.unicode.org/glossary/#code_point
6775#[ stable( feature = "rust1" , since = "1.0.0" ) ]
6876pub const MAX : char = '\u{10ffff}' ;
6977
70- /// Converts a `u32` to an `Option<char>`.
78+ /// Converts a `u32` to a `char`.
79+ ///
80+ /// Note that all [`char`]s are valid [`u32`]s, and can be casted to one with
81+ /// [`as`]:
82+ ///
83+ /// ```
84+ /// let c = '💯';
85+ /// let i = c as u32;
86+ ///
87+ /// assert_eq!(128175, i);
88+ /// ```
89+ ///
90+ /// However, the reverse is not true: not all valid [`u32`]s are valid
91+ /// [`char`]s. `from_u32()` will return `None` if the input is not a valid value
92+ /// for a [`char`].
93+ ///
94+ /// [`char`]: primitive.char.html
95+ /// [`u32`]: primitive.u32.html
96+ /// [`as`]: ../book/casting-between-types.html#as
97+ ///
98+ /// For an unsafe version of this function which ignores these checks, see
99+ /// [`from_u32_unchecked()`].
100+ ///
101+ /// [`from_u32_unchecked()`]: fn.from_u32_unchecked.html
71102///
72103/// # Examples
73104///
105+ /// Basic usage:
106+ ///
74107/// ```
75108/// use std::char;
76109///
77- /// assert_eq!(char::from_u32(0x2764), Some('❤'));
78- /// assert_eq!(char::from_u32(0x110000), None); // invalid character
110+ /// let c = char::from_u32(0x2764);
111+ ///
112+ /// assert_eq!(Some('❤'), c);
113+ /// ```
114+ ///
115+ /// Returning `None` when the input is not a valid [`char`]:
116+ ///
117+ /// ```
118+ /// use std::char;
119+ ///
120+ /// let c = char::from_u32(0x110000);
121+ ///
122+ /// assert_eq!(None, c);
79123/// ```
80124#[ inline]
81125#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -88,33 +132,104 @@ pub fn from_u32(i: u32) -> Option<char> {
88132 }
89133}
90134
91- /// Converts a `u32` to an `char`, not checking whether it is a valid unicode
92- /// codepoint.
135+ /// Converts a `u32` to a `char`, ignoring validity.
136+ ///
137+ /// Note that all [`char`]s are valid [`u32`]s, and can be casted to one with
138+ /// [`as`]:
139+ ///
140+ /// ```
141+ /// let c = '💯';
142+ /// let i = c as u32;
143+ ///
144+ /// assert_eq!(128175, i);
145+ /// ```
146+ ///
147+ /// However, the reverse is not true: not all valid [`u32`]s are valid
148+ /// [`char`]s. `from_u32_unchecked()` will ignore this, and blindly cast to
149+ /// [`char`], possibly creating an invalid one.
150+ ///
151+ /// [`char`]: primitive.char.html
152+ /// [`u32`]: primitive.u32.html
153+ /// [`as`]: ../book/casting-between-types.html#as
154+ ///
155+ /// # Safety
156+ ///
157+ /// This function is unsafe, as it may construct invalid `char` values.
158+ ///
159+ /// For a safe version of this function, see the [`from_u32()`] function.
160+ ///
161+ /// [`from_u32()`]: fn.from_u32.html
162+ ///
163+ /// # Examples
164+ ///
165+ /// Basic usage:
166+ ///
167+ /// ```
168+ /// use std::char;
169+ ///
170+ /// let c = unsafe { char::from_u32_unchecked(0x2764) };
171+ ///
172+ /// assert_eq!('❤', c);
173+ /// ```
93174#[ inline]
94175#[ stable( feature = "char_from_unchecked" , since = "1.5.0" ) ]
95176pub unsafe fn from_u32_unchecked ( i : u32 ) -> char {
96177 transmute ( i)
97178}
98179
99- /// Converts a number to the character representing it .
180+ /// Converts a digit in the given radix to a `char` .
100181///
101- /// # Return value
182+ /// A 'radix' here is sometimes also called a 'base'. A radix of two
183+ /// indicates a binary number, a radix of ten, decimal, and a radix of
184+ /// sixteen, hexicdecimal, to give some common values. Arbitrary
185+ /// radicum are supported.
102186///
103- /// Returns `Some(char )` if `num` represents one digit under `radix`,
104- /// using one character of `0-9` or `a-z`, or `None` if it doesn't .
187+ /// `from_digit( )` will return `None` if the input is not a digit in
188+ /// the given radix .
105189///
106190/// # Panics
107191///
108- /// Panics if given an ` radix` > 36.
192+ /// Panics if given a radix larger than 36.
109193///
110194/// # Examples
111195///
196+ /// Basic usage:
197+ ///
112198/// ```
113199/// use std::char;
114200///
115201/// let c = char::from_digit(4, 10);
116202///
117- /// assert_eq!(c, Some('4'));
203+ /// assert_eq!(Some('4'), c);
204+ ///
205+ /// // Decimal 11 is a single digit in base 16
206+ /// let c = char::from_digit(11, 16);
207+ ///
208+ /// assert_eq!(Some('b'), c);
209+ /// ```
210+ ///
211+ /// Returning `None` when the input is not a digit:
212+ ///
213+ /// ```
214+ /// use std::char;
215+ ///
216+ /// let c = char::from_digit(20, 10);
217+ ///
218+ /// assert_eq!(None, c);
219+ /// ```
220+ ///
221+ /// Passing a large radix, causing a panic:
222+ ///
223+ /// ```
224+ /// use std::thread;
225+ /// use std::char;
226+ ///
227+ /// let result = thread::spawn(|| {
228+ /// // this panics
229+ /// let c = char::from_digit(1, 37);
230+ /// }).join();
231+ ///
232+ /// assert!(result.is_err());
118233/// ```
119234#[ inline]
120235#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -287,8 +402,14 @@ pub fn encode_utf16_raw(mut ch: u32, dst: &mut [u16]) -> Option<usize> {
287402 }
288403}
289404
290- /// An iterator over the characters that represent a `char`, as escaped by
291- /// Rust's unicode escaping rules.
405+ /// Returns an iterator that yields the hexadecimal Unicode escape of a
406+ /// character, as `char`s.
407+ ///
408+ /// This `struct` is created by the [`escape_unicode()`] method on [`char`]. See
409+ /// its documentation for more.
410+ ///
411+ /// [`escape_unicode()`]: primitive.char.html#method.escape_unicode
412+ /// [`char`]: primitive.char.html
292413#[ derive( Clone ) ]
293414#[ stable( feature = "rust1" , since = "1.0.0" ) ]
294415pub struct EscapeUnicode {
@@ -362,8 +483,13 @@ impl Iterator for EscapeUnicode {
362483 }
363484}
364485
365- /// An iterator over the characters that represent a `char`, escaped
366- /// for maximum portability.
486+ /// An iterator that yields the literal escape code of a `char`.
487+ ///
488+ /// This `struct` is created by the [`escape_default()`] method on [`char`]. See
489+ /// its documentation for more.
490+ ///
491+ /// [`escape_default()`]: primitive.char.html#method.escape_default
492+ /// [`char`]: primitive.char.html
367493#[ derive( Clone ) ]
368494#[ stable( feature = "rust1" , since = "1.0.0" ) ]
369495pub struct EscapeDefault {
0 commit comments