pub struct Utf16String { /* private fields */ }
Expand description

A UTF-16 encoded, growable owned string.

Utf16String is a version of String that uses UTF-16 encoding instead of UTF-8 encoding. The equivalent of str for Utf16String is Utf16Str.

Unlike U16String which does not specify a coding, Utf16String is always valid UTF-16 encoding. Using unsafe methods to construct a Utf16String with invalid UTF-16 encoding results in undefined behavior.

UTF-16

Utf16String is always UTF-16. This means if you need non-UTF-16 wide strings, you should use U16String instead. It is similar, but does not constrain the encoding.

This also means you cannot directly index a single element of the string, as UTF-16 encoding may be a single u16 value or a pair of u16 surrogates. Instead, you can index subslices of the string, or use the chars iterator instead.

Examples

The easiest way to use Utf16String is with the utf16str! macro to convert string literals into UTF-16 string slices at compile time:

use widestring::{Utf16String, utf16str};
let hello = Utf16String::from(utf16str!("Hello, world!"));

Because this string is always valid UTF-16, it is a non-fallible, lossless conversion to and from standard Rust strings:

use widestring::Utf16String;
// Unlike the utf16str macro, this will do conversion at runtime instead of compile time
let hello = Utf16String::from_str("Hello, world!");
let hello_string: String = hello.to_string();
assert_eq!(hello, hello_string); // Can easily compare between string types

Implementations

Creates a new empty string.

Given that the string is empty, this will not allocate any initial buffer. While that means this initial operation is very inexpensive, it may cause excessive allocations later when you add data. If you have an idea of how much data the string will hold, consider with_capacity instead to prevent excessive re-allocation.

Creates a new empty string with a particular capacity.

This string has an internal buffer to hold its data. The capacity is the length of that buffer, and can be queried with the capacity method. This method creates and empty string, but one with an initial buffer that can hold capacity elements. This is useful when you may be appending a bunch of data to the string, reducing the number of reallocations it needs to do.

If the given capacity is 0, no allocation will occur, and this method is identical to the new method.

Converts a u16 vector to a string without checking that the string contains valid UTF-16.

See the safe version, from_vec, for more information.

Safety

This function is unsafe because it does not check that the vector passed to it is valid UTF-16. If this constraint is violated, undefined behavior results as it is assumed the Utf16String is always valid UTF-16.

Examples
use widestring::Utf16String;

let sparkle_heart = vec![0xd83d, 0xdc96]; // Raw surrogate pair
let sparkle_heart = unsafe { Utf16String::from_vec_unchecked(sparkle_heart) };

assert_eq!("💖", sparkle_heart);

Re-encodes a UTF-8–encoded string slice into a UTF-16–encoded string.

This operation is lossless and infallible, but requires a memory allocation.

Examples
use widestring::Utf16String;
let music = Utf16String::from_str("𝄞music");
assert_eq!(utf16str!("𝄞music"), music);

Converts a string into a string slice.

Converts a string into a mutable string slice.

Converts this string into a wide string of undefined encoding.

Converts a string into a vector of its elements.

This consumes the string without copying its contents.

Appends a given string slice onto the end of this string.

Examples
use widestring::Utf16String;
let mut s = Utf16String::from_str("foo");
s.push_utfstr(utf16str!("bar"));
assert_eq!(utf16str!("foobar"), s);

Returns this string’s capacity, in number of elements.

Ensures that this string’s capacity is at least additional elements larger than its length.

The capacity may be increased by more than additional elements if it chooses, to prevent frequent reallocations.

If you do not want this “at least” behavior, see the reserve_exact method.

Panics

Panics if the new capacity overflows usize.

Ensures that this string’s capacity is additional elements larger than its length.

Consider using the reserve method unless you absolutely know better than the allocator.

Panics

Panics if the new capacity overflows usize.

Shrinks the capacity of this string to match its length.

Shrinks the capacity of this string with a lower bound.

The capacity will remain at least as large as both the length and the supplied value.

If the current capacity is less than the lower limit, this is a no-op.

Returns a slice of this string’s contents.

Returns a mutable reference to the contents of this string.

Safety

This function is unsafe because it does not check that the values in the vector are valid UTF-16. If this constraint is violated, it may cause undefined beahvior with future users of the string, as it is assumed that this string is always valid UTF-16.

Returns the length of this string in number of elements, not chars or graphemes.

In other words, it might not be what a human considers the length of the string.

Returns true if this string has a length of zero, and false otherwise.

Truncates the string, removing all contents.

While this means the string will have a length of zero, it does not touch its capacity.

Converts this string into a boxed string slice.

This will drop excess capacity.

Appends a given UTF-8 string slice onto the end of this string, converting it to UTF-16.

Converts a u16 vector of UTF-16 data to a string.

Not all slices of u16 values are valid to convert, since Utf16String requires that it is always valid UTF-16. This function checks to ensure that the values are valid UTF-16, and then does the conversion. This does not do any copying.

If you are sure that the slice is valid UTF-16, and you don’t want to incur the overhead of the validity check, there is an unsafe version of this function, from_vec_unchecked, which has the same behavior but skips the check.

If you need a string slice, consider using Utf16Str::from_slice instead.

The inverse of this method is into_vec.

Errors

Returns an error if the vector is not UTF-16 with a description as to why the provided vector is not UTF-16. The error will contain the original Vec that can be reclaimed with into_vec.

Examples
use widestring::Utf16String;

let sparkle_heart = vec![0xd83d, 0xdc96]; // Raw surrogate pair
let sparkle_heart = Utf16String::from_vec(sparkle_heart).unwrap();

assert_eq!("💖", sparkle_heart);

With incorrect values that return an error:

use widestring::Utf16String;

let sparkle_heart = vec![0xd83d, 0x0]; // This is an invalid unpaired surrogate

assert!(Utf16String::from_vec(sparkle_heart).is_err());

Converts a slice of u16 data to a string, including invalid characters.

Since the given u16 slice may not be valid UTF-16, and Utf16String requires that it is always valid UTF-16, during the conversion this function replaces any invalid UTF-16 sequences with U+FFFD REPLACEMENT CHARACTER, which looks like this: �

If you are sure that the slice is valid UTF-16, and you don’t want to incur the overhead of the conversion, there is an unsafe version of this function, from_vec_unchecked, which has the same behavior but skips the checks.

This function returns a Cow<'_, Utf16Str>. If the given slice is invalid UTF-16, then we need to insert our replacement characters which will change the size of the string, and hence, require an owned Utf16String. But if it’s already valid UTF-16, we don’t need a new allocation. This return type allows us to handle both cases.

Examples
use widestring::Utf16String;

let sparkle_heart = vec![0xd83d, 0xdc96]; // Raw surrogate pair
let sparkle_heart = Utf16String::from_slice_lossy(&sparkle_heart);

assert_eq!(utf16str!("💖"), sparkle_heart);

With incorrect values that return an error:

use widestring::Utf16String;

let sparkle_heart = vec![0xd83d, 0x0]; // This is an invalid unpaired surrogate
let sparkle_heart = Utf16String::from_slice_lossy(&sparkle_heart);

assert_eq!(utf16str!("\u{fffd}\u{0000}"), sparkle_heart);

Converts a wide string of undefined encoding to a UTF-16 string without checking that the string contains valid UTF-16.

See the safe version, from_ustring, for more information.

Safety

This function is unsafe because it does not check that the string passed to it is valid UTF-16. If this constraint is violated, undefined behavior results as it is assumed the Utf16String is always valid UTF-16.

Examples
use widestring::{U16String, Utf16String};

let sparkle_heart = vec![0xd83d, 0xdc96]; // Raw surrogate pair
let sparkle_heart = U16String::from_vec(sparkle_heart);
let sparkle_heart = unsafe { Utf16String::from_ustring_unchecked(sparkle_heart) };

assert_eq!("💖", sparkle_heart);

Converts a wide string of undefined encoding into a UTF-16 string.

Not all strings with undefined encoding are valid to convert, since Utf16String requires that it is always valid UTF-16. This function checks to ensure that the string is valid UTF-16, and then does the conversion. This does not do any copying.

If you are sure that the string is valid UTF-16, and you don’t want to incur the overhead of the validity check, there is an unsafe version of this function, from_ustring_unchecked, which has the same behavior but skips the check.

If you need a string slice, consider using Utf16Str::from_ustr instead.

Errors

Returns an error if the string is not UTF-16 with a description as to why the provided string is not UTF-16.

Examples
use widestring::{U16String, Utf16String};

let sparkle_heart = vec![0xd83d, 0xdc96]; // Raw surrogate pair
let sparkle_heart = U16String::from_vec(sparkle_heart);
let sparkle_heart = Utf16String::from_ustring(sparkle_heart).unwrap();

assert_eq!("💖", sparkle_heart);

With incorrect values that return an error:

use widestring::{U16String, Utf16String};

let sparkle_heart = vec![0xd83d, 0x0]; // This is an invalid unpaired surrogate
let sparkle_heart = U16String::from_vec(sparkle_heart); // Valid for a U16String

assert!(Utf16String::from_ustring(sparkle_heart).is_err()); // But not for a Utf16String

Converts a wide string slice of undefined encoding of to a UTF-16 string, including invalid characters.

Since the given string slice may not be valid UTF-16, and Utf16String requires that it is always valid UTF-16, during the conversion this function replaces any invalid UTF-16 sequences with U+FFFD REPLACEMENT CHARACTER, which looks like this: �

If you are sure that the slice is valid UTF-16, and you don’t want to incur the overhead of the conversion, there is an unsafe version of this function, from_ustring_unchecked, which has the same behavior but skips the checks.

This function returns a Cow<'_, Utf16Str>. If the given slice is invalid UTF-16, then we need to insert our replacement characters which will change the size of the string, and hence, require an owned Utf16String. But if it’s already valid UTF-16, we don’t need a new allocation. This return type allows us to handle both cases.

Examples
use widestring::{U16Str, Utf16String};

let sparkle_heart = vec![0xd83d, 0xdc96]; // Raw surrogate pair
let sparkle_heart = U16Str::from_slice(&sparkle_heart);
let sparkle_heart = Utf16String::from_ustr_lossy(sparkle_heart);

assert_eq!(utf16str!("💖"), sparkle_heart);

With incorrect values that return an error:

use widestring::{U16Str, Utf16String};

let sparkle_heart = vec![0xd83d, 0x0]; // This is an invalid unpaired surrogate
let sparkle_heart = U16Str::from_slice(&sparkle_heart);
let sparkle_heart = Utf16String::from_ustr_lossy(sparkle_heart);

assert_eq!(utf16str!("\u{fffd}\u{0000}"), sparkle_heart);

Converts a wide C string to a UTF-16 string without checking that the string contains valid UTF-16.

The resulting string does not contain the nul terminator.

See the safe version, from_ucstring, for more information.

Safety

This function is unsafe because it does not check that the string passed to it is valid UTF-16. If this constraint is violated, undefined behavior results as it is assumed the Utf16String is always valid UTF-16.

Examples
use widestring::{U16CString, Utf16String};

let sparkle_heart = vec![0xd83d, 0xdc96]; // Raw surrogate pair
let sparkle_heart = U16CString::from_vec(sparkle_heart).unwrap();
let sparkle_heart = unsafe { Utf16String::from_ucstring_unchecked(sparkle_heart) };

assert_eq!("💖", sparkle_heart);

Converts a wide C string into a UTF-16 string.

The resulting string does not contain the nul terminator.

Not all wide C strings are valid to convert, since Utf16String requires that it is always valid UTF-16. This function checks to ensure that the string is valid UTF-16, and then does the conversion. This does not do any copying.

If you are sure that the string is valid UTF-16, and you don’t want to incur the overhead of the validity check, there is an unsafe version of this function, from_ucstring_unchecked, which has the same behavior but skips the check.

If you need a string slice, consider using Utf16Str::from_ucstr instead.

Errors

Returns an error if the string is not UTF-16 with a description as to why the provided string is not UTF-16.

Examples
use widestring::{U16CString, Utf16String};

let sparkle_heart = vec![0xd83d, 0xdc96]; // Raw surrogate pair
let sparkle_heart = U16CString::from_vec(sparkle_heart).unwrap();
let sparkle_heart = Utf16String::from_ucstring(sparkle_heart).unwrap();

assert_eq!("💖", sparkle_heart);

With incorrect values that return an error:

use widestring::{U16CString, Utf16String};

let sparkle_heart = vec![0xd83d]; // This is an invalid unpaired surrogate
let sparkle_heart = U16CString::from_vec(sparkle_heart).unwrap(); // Valid for a U16CString

assert!(Utf16String::from_ucstring(sparkle_heart).is_err()); // But not for a Utf16String

Converts a wide C string slice of to a UTF-16 string, including invalid characters.

The resulting string does not contain the nul terminator.

Since the given string slice may not be valid UTF-16, and Utf16String requires that it is always valid UTF-16, during the conversion this function replaces any invalid UTF-16 sequences with U+FFFD REPLACEMENT CHARACTER, which looks like this: �

If you are sure that the slice is valid UTF-16, and you don’t want to incur the overhead of the conversion, there is an unsafe version of this function, from_ucstring_unchecked, which has the same behavior but skips the checks.

This function returns a Cow<'_, Utf16Str>. If the given slice is invalid UTF-16, then we need to insert our replacement characters which will change the size of the string, and hence, require an owned Utf16String. But if it’s already valid UTF-16, we don’t need a new allocation. This return type allows us to handle both cases.

Examples
use widestring::{U16CStr, Utf16String};

let sparkle_heart = vec![0xd83d, 0xdc96, 0x0]; // Raw surrogate pair
let sparkle_heart = U16CStr::from_slice(&sparkle_heart).unwrap();
let sparkle_heart = Utf16String::from_ucstr_lossy(sparkle_heart);

assert_eq!(utf16str!("💖"), sparkle_heart);

With incorrect values that return an error:

use widestring::{U16CStr, Utf16String};

let sparkle_heart = vec![0xd83d, 0x0]; // This is an invalid unpaired surrogate
let sparkle_heart = U16CStr::from_slice(&sparkle_heart).unwrap();
let sparkle_heart = Utf16String::from_ucstr_lossy(sparkle_heart);

assert_eq!(utf16str!("\u{fffd}"), sparkle_heart);

Appends the given char to the end of this string.

Examples
use widestring::Utf16String;
let mut s = Utf16String::from_str("abc");

s.push('1');
s.push('2');
s.push('3');

assert_eq!("abc123", s);

Shortens this string to the specified length.

If new_len is greater than the string’s current length, this has no effect.

Note that this method has no effect on the allocated capacity of the string.

Panics

Panics if new_len does not lie on a char boundary.

Examples
use widestring::Utf16String;
let mut s = Utf16String::from_str("hello");
s.truncate(2);
assert_eq!("he", s);

Removes the last character from the string buffer and returns it.

Returns None if this string is empty.

Examples
use widestring::Utf16String;
let mut s = Utf16String::from_str("foo𝄞");

assert_eq!(s.pop(), Some('𝄞'));
assert_eq!(s.pop(), Some('o'));
assert_eq!(s.pop(), Some('o'));
assert_eq!(s.pop(), Some('f'));

assert_eq!(s.pop(), None);

Removes a char from this string at an offset and returns it.

This is an O(n) operation, as it requires copying every element in the buffer.

Panics

Panics if idx is larger than or equal to the string’s length, or if it does not lie on a char boundary.

Examples
use widestring::Utf16String;
let mut s = Utf16String::from_str("𝄞foo");

assert_eq!(s.remove(0), '𝄞');
assert_eq!(s.remove(1), 'o');
assert_eq!(s.remove(0), 'f');
assert_eq!(s.remove(0), 'o');

Retains only the characters specified by the predicate.

In other words, remove all characters c such that f(c) returns false. This method operates in place, visiting each character exactly once in the original order, and preserves the order of the retained characters.

Examples
use widestring::Utf16String;
let mut s = Utf16String::from_str("f_o_ob_ar");

s.retain(|c| c != '_');

assert_eq!(s, "foobar");

Because the elements are visited exactly once in the original order, external state may be used to decide which elements to keep.

use widestring::Utf16String;
let mut s = Utf16String::from_str("abcde");
let keep = [false, true, true, false, true];
let mut iter = keep.iter();
s.retain(|_| *iter.next().unwrap());
assert_eq!(s, "bce");

Inserts a character into this string at an offset.

This is an O(n) operation as it requires copying every element in the buffer.

Panics

Panics if idx is larger than the string’s length, or if it does not lie on a char boundary.

Examples
use widestring::Utf16String;
let mut s = Utf16String::with_capacity(5);

s.insert(0, '𝄞');
s.insert(0, 'f');
s.insert(1, 'o');
s.insert(4, 'o');

assert_eq!("fo𝄞o", s);

Inserts a UTF-16 string slice into this string at an offset.

This is an O(n) operation as it requires copying every element in the buffer.

Panics

Panics if idx is larger than the string’s length, or if it does not lie on a char boundary.

Examples
use widestring::Utf16String;
let mut s = Utf16String::from_str("bar");

s.insert_utfstr(0, utf16str!("foo"));

assert_eq!("foobar", s);

Splits the string into two at the given index.

Returns a newly allocated string. self contains elements [0, at), and the returned string contains elements [at, len). at must be on the boundary of a UTF-16 code point.

Note that the capacity of self does not change.

Panics

Panics if at is not on a UTF-16 code point boundary, or if it is beyond the last code point of the string.

Examples
use widestring::Utf16String;
let mut hello = Utf16String::from_str("Hello, World!");
let world = hello.split_off(7);
assert_eq!(hello, "Hello, ");
assert_eq!(world, "World!");

Creates a draining iterator that removes the specified range in the string and yields the removed chars.

Note: The element range is removed even if the iterator is not consumed until the end.

Panics

Panics if the starting point or end point do not lie on a char boundary, or if they’re out of bounds.

Examples

Basic usage:

use widestring::Utf16String;
let mut s = Utf16String::from_str("α is alpha, β is beta");
let beta_offset = 12;

// Remove the range up until the β from the string
let t: Utf16String = s.drain(..beta_offset).collect();
assert_eq!(t, "α is alpha, ");
assert_eq!(s, "β is beta");

// A full range clears the string
s.drain(..);
assert_eq!(s, "");

Removes the specified range in the string, and replaces it with the given string.

The given string doesn’t need to be the same length as the range.

Panics

Panics if the starting point or end point do not lie on a char boundary, or if they’re out of bounds.

Examples

Basic usage:

use widestring::{utf16str, Utf16String};
let mut s = Utf16String::from_str("α is alpha, β is beta");
let beta_offset = 12;

// Replace the range up until the β from the string
s.replace_range(..beta_offset, utf16str!("Α is capital alpha; "));
assert_eq!(s, "Α is capital alpha; β is beta");

Methods from Deref<Target = Utf16Str>

Returns an unchecked subslice of this string slice.

This is the unchecked alternative to indexing the string slice.

Safety

Callers of this function are responsible that these preconditions are satisfied:

  • The starting index must not exceed the ending index;
  • Indexes must be within bounds of the original slice;
  • Indexes must lie on UTF-16 sequence boundaries.

Failing that, the returned string slice may reference invalid memory or violate the invariants communicated by the type.

Examples
let v = utf16str!("⚧️🏳️‍⚧️➡️s");
unsafe {
    assert_eq!(utf16str!("⚧️"), v.get_unchecked(..2));
    assert_eq!(utf16str!("🏳️‍⚧️"), v.get_unchecked(2..8));
    assert_eq!(utf16str!("➡️"), v.get_unchecked(8..10));
    assert_eq!(utf16str!("s"), v.get_unchecked(10..));
}

Returns a mutable, unchecked subslice of this string slice

This is the unchecked alternative to indexing the string slice.

Safety

Callers of this function are responsible that these preconditions are satisfied:

  • The starting index must not exceed the ending index;
  • Indexes must be within bounds of the original slice;
  • Indexes must lie on UTF-16 sequence boundaries.

Failing that, the returned string slice may reference invalid memory or violate the invariants communicated by the type.

Examples
let mut v = utf16str!("⚧️🏳️‍⚧️➡️s").to_owned();
unsafe {
    assert_eq!(utf16str!("⚧️"), v.get_unchecked_mut(..2));
    assert_eq!(utf16str!("🏳️‍⚧️"), v.get_unchecked_mut(2..8));
    assert_eq!(utf16str!("➡️"), v.get_unchecked_mut(8..10));
    assert_eq!(utf16str!("s"), v.get_unchecked_mut(10..));
}

Returns the length of self.

This length is in u16 values, not chars or graphemes. In other words, it may not be what human considers the length of the string.

Examples
assert_eq!(utf16str!("foo").len(), 3);

let complex = utf16str!("⚧️🏳️‍⚧️➡️s");
assert_eq!(complex.len(), 11);
assert_eq!(complex.chars().count(), 10);

Returns true if the string has a length of zero.

Converts a string to a slice of its underlying elements.

To convert the slice back into a string slice, use the from_slice function.

Converts a mutable string to a mutable slice of its underlying elements.

Safety

This function is unsafe because you can violate the invariants of this type when mutating the slice. The caller must ensure that the contents of the slice is valid UTF before the borrow ends and the underlying string is used.

Use of this string type whose contents have been mutated to invalid UTF is undefined behavior.

Converts a string slice to a raw pointer.

This pointer will be pointing to the first element of the string slice.

The caller must ensure that the returned pointer is never written to. If you need to mutate the contents of the string slice, use as_mut_ptr.

Converts a mutable string slice to a mutable pointer.

This pointer will be pointing to the first element of the string slice.

Returns this string as a wide string slice of undefined encoding.

Returns a string slice with leading and trailing whitespace removed.

‘Whitespace’ is defined according to the terms of the Unicode Derived Core Property White_Space.

Returns a string slice with leading whitespace removed.

‘Whitespace’ is defined according to the terms of the Unicode Derived Core Property White_Space.

Text directionality

A string is a sequence of elements. start in this context means the first position of that sequence; for a left-to-right language like English or Russian, this will be left side, and for right-to-left languages like Arabic or Hebrew, this will be the right side.

Returns a string slice with trailing whitespace removed.

‘Whitespace’ is defined according to the terms of the Unicode Derived Core Property White_Space.

Text directionality

A string is a sequence of elements. end in this context means the last position of that sequence; for a left-to-right language like English or Russian, this will be right side, and for right-to-left languages like Arabic or Hebrew, this will be the left side.

Creates a new owned string by repeating this string n times.

Panics

This function will panic if the capacity would overflow.

Converts to a standard UTF-8 String.

Because this string is always valid UTF-16, the conversion is lossless and non-fallible.

Checks that index-th value is the value in a UTF-16 code point sequence or the end of the string.

Returns true if the value at index is not a UTF-16 surrogate value, or if the value at index is the first value of a surrogate pair (the “high” surrogate). Returns false if the value at index is the second value of a surrogate pair (a.k.a the “low” surrogate).

The start and end of the string (when index == self.len()) are considered to be boundaries.

Returns false if index is greater than self.len()`.

Examples
let s = utf16str!("Sparkle 💖 Heart");
assert!(s.is_char_boundary(0));

// high surrogate of `💖`
assert!(s.is_char_boundary(8));
// low surrogate of `💖`
assert!(!s.is_char_boundary(9));

assert!(s.is_char_boundary(s.len()));

Returns a subslice of this string.

This is the non-panicking alternative to indexing the string. Returns None whenever equivalent indexing operation would panic.

Examples
let v = utf16str!("⚧️🏳️‍⚧️➡️s");

assert_eq!(Some(utf16str!("⚧️")), v.get(..2));
assert_eq!(Some(utf16str!("🏳️‍⚧️")), v.get(2..8));
assert_eq!(Some(utf16str!("➡️")), v.get(8..10));
assert_eq!(Some(utf16str!("s")), v.get(10..));

assert!(v.get(3..4).is_none());

Returns a mutable subslice of this string.

This is the non-panicking alternative to indexing the string. Returns None whenever equivalent indexing operation would panic.

Examples
let mut v = utf16str!("⚧️🏳️‍⚧️➡️s").to_owned();

assert_eq!(utf16str!("⚧️"), v.get_mut(..2).unwrap());
assert_eq!(utf16str!("🏳️‍⚧️"), v.get_mut(2..8).unwrap());
assert_eq!(utf16str!("➡️"), v.get_mut(8..10).unwrap());
assert_eq!(utf16str!("s"), v.get_mut(10..).unwrap());

assert!(v.get_mut(3..4).is_none());

Divide one string slice into two at an index.

The argument, mid, should be an offset from the start of the string. It must also be on the boundary of a UTF-16 code point.

The two slices returned go from the start of the string slice to mid, and from mid to the end of the string slice.

To get mutable string slices instead, see the split_at_mut method.

Panics

Panics if mid is not on a UTF-16 code point boundary, or if it is past the end of the last code point of the string slice.

Examples
let s = utf16str!("Per Martin-Löf");

let (first, last) = s.split_at(3);

assert_eq!("Per", first);
assert_eq!(" Martin-Löf", last);

Divide one mutable string slice into two at an index.

The argument, mid, should be an offset from the start of the string. It must also be on the boundary of a UTF-16 code point.

The two slices returned go from the start of the string slice to mid, and from mid to the end of the string slice.

To get immutable string slices instead, see the split_at method.

Panics

Panics if mid is not on a UTF-16 code point boundary, or if it is past the end of the last code point of the string slice.

Examples
let mut s = utf16str!("Per Martin-Löf").to_owned();

let (first, last) = s.split_at_mut(3);

assert_eq!("Per", first);
assert_eq!(" Martin-Löf", last);

Returns an iterator over the chars of a string slice.

As this string slice consists of valid UTF-16, we can iterate through a string slice by char. This method returns such an iterator.

It’s important to remember that char represents a Unicode Scalar Value, and might not match your idea of what a ‘character’ is. Iteration over grapheme clusters may be what you actually want. This functionality is not provided by this crate.

Returns an iterator over the chars of a string slice and their positions.

As this string slice consists of valid UTF-16, we can iterate through a string slice by char. This method returns an iterator of both these chars as well as their offsets.

The iterator yields tuples. The position is first, the char is second.

An iterator over the u16 code units of a string slice.

As a UTF-16 string slice consists of a sequence of u16 code units, we can iterate through a string slice by each code unit. This method returns such an iterator.

Returns an iterator of bytes over the string encoded as UTF-8.

Returns an iterator of u32 over the sting encoded as UTF-32.

Returns an iterator that escapes each char in self with char::escape_debug.

Returns an iterator that escapes each char in self with char::escape_default.

Returns an iterator that escapes each char in self with char::escape_unicode.

Returns the lowercase equivalent of this string slice, as a new Utf16String.

‘Lowercase’ is defined according to the terms of the Unicode Derived Core Property Lowercase.

Since some characters can expand into multiple characters when changing the case, this function returns a Utf16String instead of modifying the parameter in-place.

Returns the uppercase equivalent of this string slice, as a new Utf16String.

‘Uppercase’ is defined according to the terms of the Unicode Derived Core Property Uppercase.

Since some characters can expand into multiple characters when changing the case, this function returns a Utf16String instead of modifying the parameter in-place.

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

Performs the += operation. Read more

Performs the += operation. Read more

Converts this type into a mutable reference of the (usually inferred) input type.

Converts this type into a shared reference of the (usually inferred) input type.

Converts this type into a shared reference of the (usually inferred) input type.

Converts this type into a shared reference of the (usually inferred) input type.

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

Formats the value using the given formatter. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Creates a value from an iterator. Read more

Creates a value from an iterator. Read more

Creates a value from an iterator. Read more

Creates a value from an iterator. Read more

Creates a value from an iterator. Read more

Creates a value from an iterator. Read more

Creates a value from an iterator. Read more

Creates a value from an iterator. Read more

Creates a value from an iterator. Read more

The associated error which can be returned from parsing.

Parses a string s to return a value of this type. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

Performs the mutable indexing (container[index]) operation. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Writes a string slice into this writer, returning whether the write succeeded. Read more

Writes a char into this writer, returning whether the write succeeded. Read more

Glue for usage of the write! macro with implementors of this trait. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.