Struct ArrayString
struct ArrayString<CAP: usize> { ... }
A string with a fixed capacity.
The ArrayString is a string backed by a fixed size array. It keeps track
of its length, and is parameterized by CAP for the maximum capacity.
CAP is of type usize but is range limited to u32::MAX; attempting to create larger
arrayvecs with larger capacity will panic.
The string is a contiguous value that you can store directly on the stack if needed.
Implementations
impl<CAP: usize> ArrayString<CAP>
fn new() -> ArrayString<CAP>Create a new empty
ArrayString.Capacity is inferred from the type parameter.
use ArrayString; let mut string = new; string.push_str; assert_eq!; assert_eq!;const fn new_const() -> ArrayString<CAP>Create a new empty
ArrayString(const fn).Capacity is inferred from the type parameter.
use ArrayString; static ARRAY: = new_const;const fn len(self: &Self) -> usizeReturn the length of the string.
const fn is_empty(self: &Self) -> boolReturns whether the string is empty.
fn from(s: &str) -> Result<Self, CapacityError<&str>>Create a new
ArrayStringfrom astr.Capacity is inferred from the type parameter.
Errors if the backing array is not large enough to fit the string.
use ArrayString; let mut string = from.unwrap; assert_eq!; assert_eq!; assert_eq!;fn from_byte_string(b: &[u8; CAP]) -> Result<Self, Utf8Error>Create a new
ArrayStringfrom a byte string literal.Errors if the byte string literal is not valid UTF-8.
use ArrayString; let string = from_byte_string.unwrap;fn zero_filled() -> SelfCreate a new
ArrayStringvalue fully filled with ASCII NULL characters (\0). Useful to be used as a buffer to collect external data or as a buffer for intermediate processing.use ArrayString; let string = zero_filled; assert_eq!;const fn capacity(self: &Self) -> usizeReturn the capacity of the
ArrayString.use ArrayString; let string = new; assert_eq!;const fn is_full(self: &Self) -> boolReturn if the
ArrayStringis completely filled.use ArrayString; let mut string = new; assert!; string.push_str; assert!;const fn remaining_capacity(self: &Self) -> usizeReturns the capacity left in the
ArrayString.use ArrayString; let mut string = from.unwrap; string.pop; assert_eq!;fn push(self: &mut Self, c: char)Adds the given char to the end of the string.
Panics if the backing array is not large enough to fit the additional char.
use ArrayString; let mut string = new; string.push; string.push; assert_eq!;fn try_push(self: &mut Self, c: char) -> Result<(), CapacityError<char>>Adds the given char to the end of the string.
Returns
Okif the push succeeds.Errors if the backing array is not large enough to fit the additional char.
use ArrayString; let mut string = new; string.try_push.unwrap; string.try_push.unwrap; let overflow = string.try_push; assert_eq!; assert_eq!;fn push_str(self: &mut Self, s: &str)Adds the given string slice to the end of the string.
Panics if the backing array is not large enough to fit the string.
use ArrayString; let mut string = new; string.push_str; string.push_str; assert_eq!;fn try_push_str<'a>(self: &mut Self, s: &'a str) -> Result<(), CapacityError<&'a str>>Adds the given string slice to the end of the string.
Returns
Okif the push succeeds.Errors if the backing array is not large enough to fit the string.
use ArrayString; let mut string = new; string.try_push_str.unwrap; let overflow1 = string.try_push_str; string.try_push_str.unwrap; let overflow2 = string.try_push_str; assert_eq!; assert_eq!; assert_eq!;fn pop(self: &mut Self) -> Option<char>Removes the last character from the string and returns it.
Returns
Noneif thisArrayStringis empty.use ArrayString; let mut s = from.unwrap; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn truncate(self: &mut Self, new_len: usize)Shortens this
ArrayStringto the specified length.If
new_lenis greater than the string’s current length, this has no effect.Panics if
new_lendoes not lie on acharboundary.use ArrayString; let mut string = from.unwrap; string.truncate; assert_eq!; string.truncate; assert_eq!;fn remove(self: &mut Self, idx: usize) -> charRemoves a
charfrom thisArrayStringat a byte position and returns it.This is an
O(n)operation, as it requires copying every element in the array.Panics if
idxis larger than or equal to theArrayString’s length, or if it does not lie on acharboundary.use ArrayString; let mut s = from.unwrap; assert_eq!; assert_eq!; assert_eq!;fn clear(self: &mut Self)Make the string empty.
unsafe fn set_len(self: &mut Self, length: usize)Set the strings’s length.
This function is
unsafebecause it changes the notion of the number of “valid” bytes in the string. Use with care.This method uses debug assertions to check the validity of
lengthand may use other debug assertions.fn as_str(self: &Self) -> &strReturn a string slice of the whole
ArrayString.fn as_mut_str(self: &mut Self) -> &mut strReturn a mutable string slice of the whole
ArrayString.
impl<'a, CAP: usize> TryFrom for ArrayString<CAP>
fn try_from(f: &'a str) -> Result<Self, <Self as >::Error>
impl<'a, CAP: usize> TryFrom for ArrayString<CAP>
fn try_from(f: Arguments<'a>) -> Result<Self, <Self as >::Error>
impl<CAP: usize> AsRef for ArrayString<CAP>
fn as_ref(self: &Self) -> &str
impl<CAP: usize> Borrow for ArrayString<CAP>
fn borrow(self: &Self) -> &str
impl<CAP: usize> BorrowMut for ArrayString<CAP>
fn borrow_mut(self: &mut Self) -> &mut str
impl<CAP: usize> Clone for ArrayString<CAP>
fn clone(self: &Self) -> ArrayString<CAP>fn clone_from(self: &mut Self, rhs: &Self)
impl<CAP: usize> Copy for ArrayString<CAP>
impl<CAP: usize> Debug for ArrayString<CAP>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl<CAP: usize> Default for ArrayString<CAP>
fn default() -> ArrayString<CAP>Return an empty
ArrayString
impl<CAP: usize> Deref for ArrayString<CAP>
fn deref(self: &Self) -> &str
impl<CAP: usize> DerefMut for ArrayString<CAP>
fn deref_mut(self: &mut Self) -> &mut str
impl<CAP: usize> Display for ArrayString<CAP>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl<CAP: usize> Eq for ArrayString<CAP>
impl<CAP: usize> Freeze for ArrayString<CAP>
impl<CAP: usize> FromStr for ArrayString<CAP>
fn from_str(s: &str) -> Result<Self, <Self as >::Err>
impl<CAP: usize> Hash for ArrayString<CAP>
fn hash<H: Hasher>(self: &Self, h: &mut H)
impl<CAP: usize> Ord for ArrayString<CAP>
fn cmp(self: &Self, rhs: &Self) -> Ordering
impl<CAP: usize> PartialEq for ArrayString<CAP>
fn eq(self: &Self, rhs: &Self) -> bool
impl<CAP: usize> PartialEq for ArrayString<CAP>
fn eq(self: &Self, rhs: &str) -> bool
impl<CAP: usize> PartialOrd for ArrayString<CAP>
fn partial_cmp(self: &Self, rhs: &Self) -> Option<Ordering>fn lt(self: &Self, rhs: &Self) -> boolfn le(self: &Self, rhs: &Self) -> boolfn gt(self: &Self, rhs: &Self) -> boolfn ge(self: &Self, rhs: &Self) -> bool
impl<CAP: usize> PartialOrd for ArrayString<CAP>
fn partial_cmp(self: &Self, rhs: &str) -> Option<Ordering>fn lt(self: &Self, rhs: &str) -> boolfn le(self: &Self, rhs: &str) -> boolfn gt(self: &Self, rhs: &str) -> boolfn ge(self: &Self, rhs: &str) -> bool
impl<CAP: usize> RefUnwindSafe for ArrayString<CAP>
impl<CAP: usize> Send for ArrayString<CAP>
impl<CAP: usize> Sync for ArrayString<CAP>
impl<CAP: usize> Unpin for ArrayString<CAP>
impl<CAP: usize> UnsafeUnpin for ArrayString<CAP>
impl<CAP: usize> UnwindSafe for ArrayString<CAP>
impl<CAP: usize> Write for ArrayString<CAP>
fn write_char(self: &mut Self, c: char) -> Resultfn write_str(self: &mut Self, s: &str) -> Result
impl<P, T> Receiver for ArrayString<CAP>
impl<T> Any for ArrayString<CAP>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for ArrayString<CAP>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for ArrayString<CAP>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for ArrayString<CAP>
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for ArrayString<CAP>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for ArrayString<CAP>
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T> ToString for ArrayString<CAP>
fn to_string(self: &Self) -> String
impl<T, U> Into for ArrayString<CAP>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for ArrayString<CAP>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for ArrayString<CAP>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>