Skip to content

Commit b8e0989

Browse files
committed
Add an example of std::str::encode_utf16
Closes #44419
1 parent efa3ec6 commit b8e0989

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/liballoc/str.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,19 @@ impl str {
855855
}
856856

857857
/// Returns an iterator of `u16` over the string encoded as UTF-16.
858+
///
859+
/// # Examples
860+
///
861+
/// Basic usage:
862+
///
863+
/// ```
864+
/// let text = "Zażółć gęślą jaźń";
865+
///
866+
/// let utf8_len = text.len();
867+
/// let utf16_len = text.encode_utf16().count();
868+
///
869+
/// assert!(utf16_len <= utf8_len);
870+
/// ```
858871
#[stable(feature = "encode_utf16", since = "1.8.0")]
859872
pub fn encode_utf16(&self) -> EncodeUtf16 {
860873
EncodeUtf16 { encoder: Utf16Encoder::new(self[..].chars()) }

0 commit comments

Comments
 (0)