arrayvec

  • 日期: 2022-03-08 14:40
  • 版本: 0.7.2 repo docs

一、简介

  • 提供两个结构体:ArrayVecArrayString

  • 在内存中以固定长度按照数组形式存储,并且提供操作的方法。

  • CAP参数类型虽然是usize类型,但其实最大只有u32::MAX

二、ArrayVec

2.1 结构声明


#![allow(unused)]
fn main() {
pub struct ArrayVec<T, const CAP: usize> {
    // the `len` first elements of the array are initialized
    xs: [MaybeUninit<T>; CAP],
    len: LenUint,
}
}

2.2 常用方法

  • pub fn new() -> ArrayVec<T, CAP>
  • pub fn push(&mut self, element: T)
  • pub fn try_push(&mut self, element: T) -> Result<(), CapacityError<T>>
  • pub fn insert(&mut self, index: usize, element: T)
  • pub fn pop(&mut self) -> Option<T>
  • pub fn pop_at(&mut self, index: usize) -> Option<T>
  • pub fn remove(&mut self, index: usize) -> T
  • pub const fn len(&self) -> usize
  • pub const fn is_empty(&self) -> bool
  • pub const fn capacity(&self) -> usize

三、ArrayString

3.1 结构声明


#![allow(unused)]
fn main() {
#[derive(Copy)]
pub struct ArrayString<const CAP: usize> {
    // the `len` first elements of the array are initialized
    xs: [MaybeUninit<u8>; CAP],
    len: LenUint,
}
}

3.2 常用方法

  • pub fn new() -> ArrayString<CAP>
  • pub fn from(s: &str) -> Result<Self, CapacityError<&str>>
  • pub fn push(&mut self, c: char)
  • pub fn push_str(&mut self, s: &str)
  • pub fn try_push(&mut self, c: char) -> Result<(), CapacityError<char>>
  • pub fn try_push_str<'a>(&mut self,s: &'a str) -> Result<(), CapacityError<&'a str>>
  • pub fn pop(&mut self) -> Option<char>
  • pub fn truncate(&mut self, new_len: usize)
  • pub fn clear(&mut self)
  • pub fn remove(&mut self, idx: usize) -> char
  • pub const fn len(&self) -> usize
  • pub const fn is_empty(&self) -> bool
  • pub const fn capacity(&self) -> usize
  • pub const fn is_full(&self) -> bool