Struct arrayvec::ArrayString
source · [−]pub struct ArrayString<const CAP: usize> { /* private fields */ }
Expand description
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
sourceimpl<const CAP: usize> ArrayString<CAP>
impl<const CAP: usize> ArrayString<CAP>
sourcepub fn new() -> ArrayString<CAP>
pub 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);
sourcepub const fn new_const() -> ArrayString<CAP>
pub 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();
sourcepub fn from(s: &str) -> Result<Self, CapacityError<&str>>
pub 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);
sourcepub fn from_byte_string(b: &[u8; CAP]) -> Result<Self, Utf8Error>
pub 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();
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Return the capacity of the ArrayString
.
use arrayvec::ArrayString;
let string = ArrayString::<3>::new();
assert_eq!(string.capacity(), 3);
sourcepub fn is_full(&self) -> bool
pub 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());
sourcepub fn push(&mut self, c: char)
pub 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");
sourcepub fn try_push(&mut self, c: char) -> Result<(), CapacityError<char>>
pub 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');
sourcepub fn push_str(&mut self, s: &str)
pub 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");
sourcepub fn try_push_str<'a>(
&mut self,
s: &'a str
) -> Result<(), CapacityError<&'a str>>
pub 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");
sourcepub fn pop(&mut self) -> Option<char>
pub 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);
sourcepub fn truncate(&mut self, new_len: usize)
pub 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");
sourcepub fn remove(&mut self, idx: usize) -> char
pub 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');
Trait Implementations
sourceimpl<const CAP: usize> AsRef<str> for ArrayString<CAP>
impl<const CAP: usize> AsRef<str> for ArrayString<CAP>
sourceimpl<const CAP: usize> Borrow<str> for ArrayString<CAP>
impl<const CAP: usize> Borrow<str> for ArrayString<CAP>
sourceimpl<const CAP: usize> Clone for ArrayString<CAP>
impl<const CAP: usize> Clone for ArrayString<CAP>
sourcefn clone(&self) -> ArrayString<CAP>
fn clone(&self) -> ArrayString<CAP>
Returns a copy of the value. Read more
sourcefn clone_from(&mut self, rhs: &Self)
fn clone_from(&mut self, rhs: &Self)
Performs copy-assignment from source
. Read more
sourceimpl<const CAP: usize> Debug for ArrayString<CAP>
impl<const CAP: usize> Debug for ArrayString<CAP>
sourceimpl<const CAP: usize> Default for ArrayString<CAP>
impl<const CAP: usize> Default for ArrayString<CAP>
sourcefn default() -> ArrayString<CAP>
fn default() -> ArrayString<CAP>
Return an empty ArrayString
sourceimpl<const CAP: usize> Deref for ArrayString<CAP>
impl<const CAP: usize> Deref for ArrayString<CAP>
sourceimpl<const CAP: usize> DerefMut for ArrayString<CAP>
impl<const CAP: usize> DerefMut for ArrayString<CAP>
sourceimpl<const CAP: usize> Display for ArrayString<CAP>
impl<const CAP: usize> Display for ArrayString<CAP>
sourceimpl<const CAP: usize> FromStr for ArrayString<CAP>
impl<const CAP: usize> FromStr for ArrayString<CAP>
sourceimpl<const CAP: usize> Hash for ArrayString<CAP>
impl<const CAP: usize> Hash for ArrayString<CAP>
sourceimpl<const CAP: usize> Ord for ArrayString<CAP>
impl<const CAP: usize> Ord for ArrayString<CAP>
sourceimpl<const CAP: usize> PartialEq<ArrayString<CAP>> for ArrayString<CAP>
impl<const CAP: usize> PartialEq<ArrayString<CAP>> for ArrayString<CAP>
sourceimpl<const CAP: usize> PartialEq<ArrayString<CAP>> for str
impl<const CAP: usize> PartialEq<ArrayString<CAP>> for str
sourceimpl<const CAP: usize> PartialEq<str> for ArrayString<CAP>
impl<const CAP: usize> PartialEq<str> for ArrayString<CAP>
sourceimpl<const CAP: usize> PartialOrd<ArrayString<CAP>> for ArrayString<CAP>
impl<const CAP: usize> PartialOrd<ArrayString<CAP>> for ArrayString<CAP>
sourcefn partial_cmp(&self, rhs: &Self) -> Option<Ordering>
fn partial_cmp(&self, rhs: &Self) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
sourcefn lt(&self, rhs: &Self) -> bool
fn lt(&self, rhs: &Self) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
sourcefn le(&self, rhs: &Self) -> bool
fn le(&self, rhs: &Self) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl<const CAP: usize> PartialOrd<ArrayString<CAP>> for str
impl<const CAP: usize> PartialOrd<ArrayString<CAP>> for str
sourcefn partial_cmp(&self, rhs: &ArrayString<CAP>) -> Option<Ordering>
fn partial_cmp(&self, rhs: &ArrayString<CAP>) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
sourcefn lt(&self, rhs: &ArrayString<CAP>) -> bool
fn lt(&self, rhs: &ArrayString<CAP>) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
sourcefn le(&self, rhs: &ArrayString<CAP>) -> bool
fn le(&self, rhs: &ArrayString<CAP>) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourcefn gt(&self, rhs: &ArrayString<CAP>) -> bool
fn gt(&self, rhs: &ArrayString<CAP>) -> bool
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
sourcefn ge(&self, rhs: &ArrayString<CAP>) -> bool
fn ge(&self, rhs: &ArrayString<CAP>) -> bool
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
sourceimpl<const CAP: usize> PartialOrd<str> for ArrayString<CAP>
impl<const CAP: usize> PartialOrd<str> for ArrayString<CAP>
sourcefn partial_cmp(&self, rhs: &str) -> Option<Ordering>
fn partial_cmp(&self, rhs: &str) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
sourcefn lt(&self, rhs: &str) -> bool
fn lt(&self, rhs: &str) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
sourcefn le(&self, rhs: &str) -> bool
fn le(&self, rhs: &str) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl<'a, const CAP: usize> TryFrom<&'a str> for ArrayString<CAP>
impl<'a, const CAP: usize> TryFrom<&'a str> for ArrayString<CAP>
sourceimpl<'a, const CAP: usize> TryFrom<Arguments<'a>> for ArrayString<CAP>
impl<'a, const CAP: usize> TryFrom<Arguments<'a>> for ArrayString<CAP>
sourceimpl<const CAP: usize> Write for ArrayString<CAP>
impl<const CAP: usize> Write for ArrayString<CAP>
Write
appends written data to the end of the string.
impl<const CAP: usize> Copy for ArrayString<CAP>
impl<const CAP: usize> Eq for ArrayString<CAP>
Auto Trait Implementations
impl<const CAP: usize> RefUnwindSafe for ArrayString<CAP>
impl<const CAP: usize> Send for ArrayString<CAP>
impl<const CAP: usize> Sync for ArrayString<CAP>
impl<const CAP: usize> Unpin for ArrayString<CAP>
impl<const CAP: usize> UnwindSafe for ArrayString<CAP>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more