Trait core::char::CharExtStable [-]  [+] [src]

pub trait CharExt {
    fn is_digit(self, radix: usize) -> bool;
    fn to_digit(self, radix: usize) -> Option<usize>;
    fn escape_unicode(self) -> EscapeUnicode;
    fn escape_default(self) -> EscapeDefault;
    fn len_utf8(self) -> usize;
    fn len_utf16(self) -> usize;
    fn encode_utf8(self, dst: &mut [u8]) -> Option<usize>;
    fn encode_utf16(self, dst: &mut [u16]) -> Option<usize>;
}

Basic char manipulations.

Required Methods

fn is_digit(self, radix: usize) -> bool

Checks if a char parses as a numeric digit in the given radix.

Compared to is_numeric(), this function only recognizes the characters 0-9, a-z and A-Z.

Return value

Returns true if c is a valid digit under radix, and false otherwise.

Panics

Panics if given a radix > 36.

Examples

fn main() { let c = '1'; assert!(c.is_digit(10)); assert!('f'.is_digit(16)); }
let c = '1';

assert!(c.is_digit(10));

assert!('f'.is_digit(16));

fn to_digit(self, radix: usize) -> Option<usize>

Converts a character to the corresponding digit.

Return value

If c is between '0' and '9', the corresponding value between 0 and 9. If c is 'a' or 'A', 10. If c is 'b' or 'B', 11, etc. Returns none if the character does not refer to a digit in the given radix.

Panics

Panics if given a radix outside the range [0..36].

Examples

fn main() { let c = '1'; assert_eq!(c.to_digit(10), Some(1)); assert_eq!('f'.to_digit(16), Some(15)); }
let c = '1';

assert_eq!(c.to_digit(10), Some(1));

assert_eq!('f'.to_digit(16), Some(15));

fn escape_unicode(self) -> EscapeUnicode

Returns an iterator that yields the hexadecimal Unicode escape of a character, as chars.

All characters are escaped with Rust syntax of the form \\u{NNNN} where NNNN is the shortest hexadecimal representation of the code point.

Examples

fn main() { for i in '❤'.escape_unicode() { println!("{}", i); } }
for i in '❤'.escape_unicode() {
    println!("{}", i);
}

This prints:

\
u
{
2
7
6
4
}

Collecting into a String:

fn main() { let heart: String = '❤'.escape_unicode().collect(); assert_eq!(heart, r"\u{2764}"); }
let heart: String = '❤'.escape_unicode().collect();

assert_eq!(heart, r"\u{2764}");

fn escape_default(self) -> EscapeDefault

Returns an iterator that yields the 'default' ASCII and C++11-like literal escape of a character, as chars.

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 languages. The exact rules are:

  • Tab, CR and LF are escaped as '\t', '\r' and '\n' respectively.
  • Single-quote, double-quote and backslash chars are backslash- escaped.
  • Any other chars in the range [0x20,0x7e] are not escaped.
  • Any other chars are given hex Unicode escapes; see escape_unicode.

Examples

fn main() { for i in '"'.escape_default() { println!("{}", i); } }
for i in '"'.escape_default() {
    println!("{}", i);
}

This prints:

\
"

Collecting into a String:

fn main() { let quote: String = '"'.escape_default().collect(); assert_eq!(quote, "\\\""); }
let quote: String = '"'.escape_default().collect();

assert_eq!(quote, "\\\"");

fn len_utf8(self) -> usize

Returns the number of bytes this character would need if encoded in UTF-8.

Examples

fn main() { let n = 'ß'.len_utf8(); assert_eq!(n, 2); }
let n = 'ß'.len_utf8();

assert_eq!(n, 2);

fn len_utf16(self) -> usize

Returns the number of bytes this character would need if encoded in UTF-16.

Examples

fn main() { let n = 'ß'.len_utf16(); assert_eq!(n, 1); }
let n = 'ß'.len_utf16();

assert_eq!(n, 1);

fn encode_utf8(self, dst: &mut [u8]) -> Option<usize>

Encodes this character as UTF-8 into the provided byte buffer, and then returns the number of bytes written.

If the buffer is not large enough, nothing will be written into it and a None will be returned.

Examples

In both of these examples, 'ß' takes two bytes to encode.

fn main() { let mut b = [0; 2]; let result = 'ß'.encode_utf8(&mut b); assert_eq!(result, Some(2)); }
let mut b = [0; 2];

let result = 'ß'.encode_utf8(&mut b);

assert_eq!(result, Some(2));

A buffer that's too small:

fn main() { let mut b = [0; 1]; let result = 'ß'.encode_utf8(&mut b); assert_eq!(result, None); }
let mut b = [0; 1];

let result = 'ß'.encode_utf8(&mut b);

assert_eq!(result, None);

fn encode_utf16(self, dst: &mut [u16]) -> Option<usize>

Encodes this character as UTF-16 into the provided u16 buffer, and then returns the number of u16s written.

If the buffer is not large enough, nothing will be written into it and a None will be returned.

Examples

In both of these examples, 'ß' takes one byte to encode.

fn main() { let mut b = [0; 1]; let result = 'ß'.encode_utf16(&mut b); assert_eq!(result, Some(1)); }
let mut b = [0; 1];

let result = 'ß'.encode_utf16(&mut b);

assert_eq!(result, Some(1));

A buffer that's too small:

fn main() { let mut b = [0; 0]; let result = 'ß'.encode_utf8(&mut b); assert_eq!(result, None); }
let mut b = [0; 0];

let result = 'ß'.encode_utf8(&mut b);

assert_eq!(result, None);

Implementors