std_unicode::char::decode_utf16 - Rust
Description: API documentation for the Rust `decode_utf16` fn in crate `std_unicode`.
Keywords: rust, rustlang, rust-lang, decode_utf16

# Function std_unicode::char::decode_utf16 1.9.0 [−] [src]

ⓘImportant traits for DecodeUtf16<I>

### Important traits for DecodeUtf16<I>

`impl<I: Iterator<Item = u16>> Iterator for DecodeUtf16<I> type Item = Result<char, DecodeUtf16Error>;`

    pub fn decode_utf16<I: IntoIterator<Item = u16>>(
        iter: I
    ) -> DecodeUtf16<I::IntoIter>

Create an iterator over the UTF-16 encoded code points in `iter`, returning unpaired surrogates as `Err`s.

# Examples

Basic usage:

    use std::char::decode_utf16;

    fn main() {
        // 𝄞mus<invalid>ic<invalid>
        let v = [0xD834, 0xDD1E, 0x006d, 0x0075,
                 0x0073, 0xDD1E, 0x0069, 0x0063,
                 0xD834];

        assert_eq!(decode_utf16(v.iter().cloned())
                               .map(|r| r.map_err(|e| e.unpaired_surrogate()))
                               .collect::<Vec<_>>(),
                   vec![Ok('𝄞'),
                        Ok('m'), Ok('u'), Ok('s'),
                        Err(0xDD1E),
                        Ok('i'), Ok('c'),
                        Err(0xD834)]);
    }Run

A lossy decoder can be obtained by replacing `Err` results with the replacement character:

    use std::char::{decode_utf16, REPLACEMENT_CHARACTER};

    fn main() {
        // 𝄞mus<invalid>ic<invalid>
        let v = [0xD834, 0xDD1E, 0x006d, 0x0075,
                 0x0073, 0xDD1E, 0x0069, 0x0063,
                 0xD834];

        assert_eq!(decode_utf16(v.iter().cloned())
                       .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))
                       .collect::<String>(),
                   "𝄞mus�ic�");
    }Run

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
↑
  Move up in search results
↓
  Move down in search results
↹
  Switch tab
⏎
  Go to active search result
+ / -
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)