Struct ArrayString

#[repr(C)]
pub struct ArrayString<const CAP: usize> { /* private fields */ }

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 (or u16 on 16-bit targets); 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<const CAP: usize> ArrayString<CAP>

fn new() -> ArrayString<CAP>

Create a new empty ArrayString.

Capacity is inferred from the type parameter.

use arrayvec::ArrayString;

let mut string = ArrayString::<16>::new();
string.push_str("foo");
assert_eq!(&string[..], "foo");
assert_eq!(string.capacity(), 16);
const fn new_const() -> ArrayString<CAP>

Create a new empty ArrayString (const fn).

Capacity is inferred from the type parameter.

use arrayvec::ArrayString;

static ARRAY: ArrayString<1024> = ArrayString::new_const();
const fn len(&self) -> usize

Return the length of the string.

const fn is_empty(&self) -> bool

Returns whether the string is empty.

fn from(s: &str) -> Result<Self, CapacityError<&str>>

Create a new ArrayString from a str.

Capacity is inferred from the type parameter.

Errors if the backing array is not large enough to fit the string.

use arrayvec::ArrayString;

let mut string = ArrayString::<3>::from("foo").unwrap();
assert_eq!(&string[..], "foo");
assert_eq!(string.len(), 3);
assert_eq!(string.capacity(), 3);
fn from_byte_string(b: &[u8; CAP]) -> Result<Self, Utf8Error>

Create a new ArrayString from a byte string literal.

Errors if the byte string literal is not valid UTF-8.

use arrayvec::ArrayString;

let string = ArrayString::from_byte_string(b"hello world").unwrap();
fn zero_filled() -> Self

Create a new ArrayString value 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 arrayvec::ArrayString;

let string = ArrayString::<16>::zero_filled();
assert_eq!(string.len(), 16);
const fn capacity(&self) -> usize

Return the capacity of the ArrayString.

use arrayvec::ArrayString;

let string = ArrayString::<3>::new();
assert_eq!(string.capacity(), 3);
const fn is_full(&self) -> bool

Return if the ArrayString is completely filled.

use arrayvec::ArrayString;

let mut string = ArrayString::<1>::new();
assert!(!string.is_full());
string.push_str("A");
assert!(string.is_full());
const fn remaining_capacity(&self) -> usize

Returns the capacity left in the ArrayString.

use arrayvec::ArrayString;

let mut string = ArrayString::<3>::from("abc").unwrap();
string.pop();
assert_eq!(string.remaining_capacity(), 1);
fn push(&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 arrayvec::ArrayString;

let mut string = ArrayString::<2>::new();

string.push('a');
string.push('b');

assert_eq!(&string[..], "ab");
fn try_push(&mut self, c: char) -> Result<(), CapacityError<char>>

Adds the given char to the end of the string.

Returns Ok if the push succeeds.

Errors if the backing array is not large enough to fit the additional char.

use arrayvec::ArrayString;

let mut string = ArrayString::<2>::new();

string.try_push('a').unwrap();
string.try_push('b').unwrap();
let overflow = string.try_push('c');

assert_eq!(&string[..], "ab");
assert_eq!(overflow.unwrap_err().element(), 'c');
fn push_str(&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 arrayvec::ArrayString;

let mut string = ArrayString::<2>::new();

string.push_str("a");
string.push_str("d");

assert_eq!(&string[..], "ad");
fn try_push_str<'a>(&mut self, s: &'a str) -> Result<(), CapacityError<&'a str>>

Adds the given string slice to the end of the string.

Returns Ok if the push succeeds.

Errors if the backing array is not large enough to fit the string.

use arrayvec::ArrayString;

let mut string = ArrayString::<2>::new();

string.try_push_str("a").unwrap();
let overflow1 = string.try_push_str("bc");
string.try_push_str("d").unwrap();
let overflow2 = string.try_push_str("ef");

assert_eq!(&string[..], "ad");
assert_eq!(overflow1.unwrap_err().element(), "bc");
assert_eq!(overflow2.unwrap_err().element(), "ef");
fn pop(&mut self) -> Option<char>

Removes the last character from the string and returns it.

Returns None if this ArrayString is empty.

use arrayvec::ArrayString;
 
let mut s = ArrayString::<3>::from("foo").unwrap();

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

assert_eq!(s.pop(), None);
fn truncate(&mut self, new_len: usize)

Shortens this ArrayString to the specified length.

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

Panics if new_len does not lie on a char boundary.

use arrayvec::ArrayString;

let mut string = ArrayString::<6>::from("foobar").unwrap();
string.truncate(3);
assert_eq!(&string[..], "foo");
string.truncate(4);
assert_eq!(&string[..], "foo");
fn remove(&mut self, idx: usize) -> char

Removes a char from this ArrayString at a byte position and returns it.

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

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

use arrayvec::ArrayString;
 
let mut s = ArrayString::<3>::from("foo").unwrap();

assert_eq!(s.remove(0), 'f');
assert_eq!(s.remove(1), 'o');
assert_eq!(s.remove(0), 'o');
fn clear(&mut self)

Make the string empty.

unsafe fn set_len(&mut self, length: usize)

Set the strings’s length.

This function is unsafe because 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 length and may use other debug assertions.

fn as_str(&self) -> &str

Return a string slice of the whole ArrayString.

fn as_mut_str(&mut self) -> &mut str

Return a mutable string slice of the whole ArrayString.

fn as_ptr(&self) -> *const u8

Return a raw pointer to the string's buffer.

fn as_mut_ptr(&mut self) -> *mut u8

Return a raw mutable pointer to the string's buffer.

Trait Implementations

impl<'a, const CAP: usize> TryFrom<&'a str> for ArrayString<CAP>

type Error = CapacityError<&'a str>;
fn try_from(f: &'a str) -> Result<Self, Self::Error>

impl<'a, const CAP: usize> TryFrom<Arguments<'a>> for ArrayString<CAP>

type Error = CapacityError<Error>;
fn try_from(f: Arguments<'a>) -> Result<Self, Self::Error>

impl<const CAP: usize> AsRef<Path> for ArrayString<CAP>

fn as_ref(&self) -> &Path

impl<const CAP: usize> AsRef<str> for ArrayString<CAP>

fn as_ref(&self) -> &str

impl<const CAP: usize> Borrow<str> for ArrayString<CAP>

fn borrow(&self) -> &str

impl<const CAP: usize> BorrowMut<str> for ArrayString<CAP>

fn borrow_mut(&mut self) -> &mut str

impl<const CAP: usize> Clone for ArrayString<CAP>

fn clone(&self) -> ArrayString<CAP>
fn clone_from(&mut self, rhs: &Self)

impl<const CAP: usize> Copy for ArrayString<CAP>

impl<const CAP: usize> Debug for ArrayString<CAP>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl<const CAP: usize> Default for ArrayString<CAP>

fn default() -> ArrayString<CAP>

Return an empty ArrayString

impl<const CAP: usize> Deref for ArrayString<CAP>

type Target = str;
fn deref(&self) -> &str

impl<const CAP: usize> DerefMut for ArrayString<CAP>

fn deref_mut(&mut self) -> &mut str

impl<const CAP: usize> Display for ArrayString<CAP>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl<const CAP: usize> Eq for ArrayString<CAP>

impl<const CAP: usize> FromStr for ArrayString<CAP>

type Err = CapacityError;
fn from_str(s: &str) -> Result<Self, Self::Err>

impl<const CAP: usize> Hash for ArrayString<CAP>

fn hash<H: Hasher>(&self, h: &mut H)

impl<const CAP: usize> Ord for ArrayString<CAP>

fn cmp(&self, rhs: &Self) -> Ordering

impl<const CAP: usize> PartialEq for ArrayString<CAP>

fn eq(&self, rhs: &Self) -> bool

impl<const CAP: usize> PartialEq<str> for ArrayString<CAP>

fn eq(&self, rhs: &str) -> bool

impl<const CAP: usize> PartialOrd for ArrayString<CAP>

fn partial_cmp(&self, rhs: &Self) -> Option<Ordering>
fn lt(&self, rhs: &Self) -> bool
fn le(&self, rhs: &Self) -> bool
fn gt(&self, rhs: &Self) -> bool
fn ge(&self, rhs: &Self) -> bool

impl<const CAP: usize> PartialOrd<str> for ArrayString<CAP>

fn partial_cmp(&self, rhs: &str) -> Option<Ordering>
fn lt(&self, rhs: &str) -> bool
fn le(&self, rhs: &str) -> bool
fn gt(&self, rhs: &str) -> bool
fn ge(&self, rhs: &str) -> bool

impl<const CAP: usize> Write for ArrayString<CAP>

fn write_char(&mut self, c: char) -> Result
fn write_str(&mut self, s: &str) -> Result

Auto Trait Implementations

impl<const CAP: usize> Freeze for ArrayString<CAP> where [MaybeUninit<u8>; CAP]: Freeze,

impl<const CAP: usize> RefUnwindSafe for ArrayString<CAP> where [MaybeUninit<u8>; CAP]: RefUnwindSafe,

impl<const CAP: usize> Send for ArrayString<CAP> where [MaybeUninit<u8>; CAP]: Send,

impl<const CAP: usize> Sync for ArrayString<CAP> where [MaybeUninit<u8>; CAP]: Sync,

impl<const CAP: usize> Unpin for ArrayString<CAP> where [MaybeUninit<u8>; CAP]: Unpin,

impl<const CAP: usize> UnsafeUnpin for ArrayString<CAP> where [MaybeUninit<u8>; CAP]: UnsafeUnpin,

impl<const CAP: usize> UnwindSafe for ArrayString<CAP> where [MaybeUninit<u8>; CAP]: UnwindSafe,

Blanket Implementations

impl<P, T> Receiver for ArrayString<CAP> where P: Deref<Target = T> + ?Sized, T: ?Sized,

type Target = T;

impl<T> Any for ArrayString<CAP> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for ArrayString<CAP> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for ArrayString<CAP> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> CloneToUninit for ArrayString<CAP> where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for ArrayString<CAP>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for ArrayString<CAP> where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T> ToString for ArrayString<CAP> where T: Display + ?Sized,

fn to_string(&self) -> String

impl<T, U> Into<U> for ArrayString<CAP> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom<U> for ArrayString<CAP> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for ArrayString<CAP> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>