diff options
| author | Steve Klabnik <steve@steveklabnik.com> | 2015-03-24 15:47:47 -0400 |
|---|---|---|
| committer | Steve Klabnik <steve@steveklabnik.com> | 2015-03-24 15:47:47 -0400 |
| commit | 528a5e2baec813866365ead4240e88e7eb231273 (patch) | |
| tree | 9d858c4c94bc0a46b68560349cc327e54c361c77 /src/libstd | |
| parent | 28a0b25f424090255966273994748a9f9901059f (diff) | |
| download | rust-528a5e2baec813866365ead4240e88e7eb231273.tar.gz rust-528a5e2baec813866365ead4240e88e7eb231273.zip | |
Add examples for std::ascii
Also tweaked a few things.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/ascii.rs | 100 |
1 files changed, 96 insertions, 4 deletions
diff --git a/src/libstd/ascii.rs b/src/libstd/ascii.rs index 93215090d95..cb406275455 100644 --- a/src/libstd/ascii.rs +++ b/src/libstd/ascii.rs @@ -34,7 +34,7 @@ pub trait OwnedAsciiExt { fn into_ascii_lowercase(self) -> Self; } -/// Extension methods for ASCII-subset only operations on string slices +/// Extension methods for ASCII-subset only operations on string slices. #[stable(feature = "rust1", since = "1.0.0")] pub trait AsciiExt { /// Container type for copied ASCII characters. @@ -42,36 +42,114 @@ pub trait AsciiExt { type Owned; /// Check if within the ASCII range. + /// + /// # Examples + /// + /// ``` + /// use std::ascii::AsciiExt; + /// + /// let ascii = 'a'; + /// let utf8 = '❤'; + /// + /// assert_eq!(true, ascii.is_ascii()); + /// assert_eq!(false, utf8.is_ascii()) + /// ``` #[stable(feature = "rust1", since = "1.0.0")] fn is_ascii(&self) -> bool; - /// Makes a copy of the string in ASCII upper case: + /// Makes a copy of the string in ASCII upper case. + /// /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z', /// but non-ASCII letters are unchanged. + /// + /// # Examples + /// + /// ``` + /// use std::ascii::AsciiExt; + /// + /// let ascii = 'a'; + /// let utf8 = '❤'; + /// + /// assert_eq!('A', ascii.to_ascii_uppercase()); + /// assert_eq!('❤', utf8.to_ascii_uppercase()); + /// ``` #[stable(feature = "rust1", since = "1.0.0")] fn to_ascii_uppercase(&self) -> Self::Owned; - /// Makes a copy of the string in ASCII lower case: + /// Makes a copy of the string in ASCII lower case. + /// /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z', /// but non-ASCII letters are unchanged. + /// + /// # Examples + /// + /// ``` + /// use std::ascii::AsciiExt; + /// + /// let ascii = 'A'; + /// let utf8 = '❤'; + /// + /// assert_eq!('a', ascii.to_ascii_lowercase()); + /// assert_eq!('❤', utf8.to_ascii_lowercase()); + /// ``` #[stable(feature = "rust1", since = "1.0.0")] fn to_ascii_lowercase(&self) -> Self::Owned; /// Check that two strings are an ASCII case-insensitive match. + /// /// Same as `to_ascii_lowercase(a) == to_ascii_lowercase(b)`, /// but without allocating and copying temporary strings. + /// + /// # Examples + /// + /// ``` + /// use std::ascii::AsciiExt; + /// + /// let ascii1 = 'A'; + /// let ascii2 = 'a'; + /// let ascii3 = 'A'; + /// let ascii4 = 'z'; + /// + /// assert_eq!(true, ascii1.eq_ignore_ascii_case(&ascii2)); + /// assert_eq!(true, ascii1.eq_ignore_ascii_case(&ascii3)); + /// assert_eq!(false, ascii1.eq_ignore_ascii_case(&ascii4)); + /// ``` #[stable(feature = "rust1", since = "1.0.0")] fn eq_ignore_ascii_case(&self, other: &Self) -> bool; /// Convert this type to its ASCII upper case equivalent in-place. /// /// See `to_ascii_uppercase` for more information. + /// + /// # Examples + /// + /// ``` + /// use std::ascii::AsciiExt; + /// + /// let mut ascii = 'a'; + /// + /// ascii.make_ascii_uppercase(); + /// + /// assert_eq!('A', ascii); + /// ``` #[unstable(feature = "ascii")] fn make_ascii_uppercase(&mut self); /// Convert this type to its ASCII lower case equivalent in-place. /// /// See `to_ascii_lowercase` for more information. + /// + /// # Examples + /// + /// ``` + /// use std::ascii::AsciiExt; + /// + /// let mut ascii = 'A'; + /// + /// ascii.make_ascii_lowercase(); + /// + /// assert_eq!('a', ascii); + /// ``` #[unstable(feature = "ascii")] fn make_ascii_lowercase(&mut self); } @@ -246,7 +324,7 @@ pub struct EscapeDefault { data: [u8; 4], } -/// Returns a 'default' ASCII and C++11-like literal escape of a `u8` +/// Returns an iterator that produces an escaped version of a `u8`. /// /// The default is chosen with a bias toward producing literals that are /// legal in a variety of languages, including C++11 and similar C-family @@ -257,6 +335,20 @@ pub struct EscapeDefault { /// - Any other chars in the range [0x20,0x7e] are not escaped. /// - Any other chars are given hex escapes of the form '\xNN'. /// - Unicode escapes are never generated by this function. +/// +/// # Examples +/// +/// ``` +/// use std::ascii; +/// +/// let escaped = ascii::escape_default(b'0').next().unwrap(); +/// assert_eq!(b'0', escaped); +/// +/// let mut escaped = ascii::escape_default(b'\t'); +/// +/// assert_eq!(b'\\', escaped.next().unwrap()); +/// assert_eq!(b't', escaped.next().unwrap()); +/// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn escape_default(c: u8) -> EscapeDefault { let (data, len) = match c { |
