pub struct StringArray<const N: usize>(/* private fields */);
Expand description

An array containing a null-terminated string.

§Equality / Hashing

For the purposes of comparing and hashing array strings, any characters after the first null terminator are ignored. The below example demonstrates this property with two strings that differ in the characters that come after the first null terminators.

let string1 = StringArray::<3>::new([0x61, 0, 0]);
let string2 = StringArray::<3>::new([0x61, 0, 0x61]);

assert_eq!(string1, string2);

let mut hasher1 = DefaultHasher::new();
string1.hash(&mut hasher1);
let mut hasher2 = DefaultHasher::new();
string2.hash(&mut hasher2);

assert_eq!(hasher1.finish(), hasher2.finish());

Implementations§

source§

impl<const N: usize> StringArray<N>

source

pub fn new(array: [c_char; N]) -> Self

Constructs a string array from a character array.

§Panics
  • characters does not contain a null-terminator
source

pub const fn from_bytes(bytes: &[u8]) -> Self

Constructs a string array from a byte string.

If the byte string is longer than N - 1, then the byte string will be truncated to fit inside of the constructed string array (the last character is reserved for a null terminator). The constructed string array will always be null-terminated regardless if the byte string is or is not null-terminated.

source

pub fn from_cstr(cstr: &CStr) -> Self

Constructs a string array from a borrowed C string.

If the borrowed C string is longer than N - 1, then the borrowed C string will be truncated to fit inside of the constructed string array (the last character is reserved for a null terminator).

source

pub unsafe fn from_ptr(ptr: *const c_char) -> Self

Constructs a string array from a pointer to a null-terminated string.

If the null-terminated string is longer than N - 1, then the null-terminated string will be truncated to fit inside of the constructed string array (the last character is reserved for a null terminator).

§Safety
  • ptr must be a pointer to a null-terminated string
source

pub fn as_array(&self) -> &[c_char; N]

Gets the underlying character array for this string array.

source

pub fn as_bytes(&self) -> &[u8]

Gets this string array as a slice of bytes.

source

pub fn as_cstr(&self) -> &CStr

Gets this string array as a borrowed C string.

source

pub fn to_string_lossy(&self) -> Cow<'_, str>

Converts this string array to a UTF-8 string (lossily).

Methods from Deref<Target = [c_char; N]>§

1.57.0 · source

pub fn as_slice(&self) -> &[T]

Returns a slice containing the entire array. Equivalent to &s[..].

1.77.0 · source

pub fn each_ref(&self) -> [&T; N]

Borrows each element and returns an array of references with the same size as self.

§Example
let floats = [3.1, 2.7, -1.0];
let float_refs: [&f64; 3] = floats.each_ref();
assert_eq!(float_refs, [&3.1, &2.7, &-1.0]);

This method is particularly useful if combined with other methods, like map. This way, you can avoid moving the original array if its elements are not Copy.

let strings = ["Ferris".to_string(), "♥".to_string(), "Rust".to_string()];
let is_ascii = strings.each_ref().map(|s| s.is_ascii());
assert_eq!(is_ascii, [true, false, true]);

// We can still access the original array: it has not been moved.
assert_eq!(strings.len(), 3);
source

pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])

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

Divides one array reference into two at an index.

The first will contain all indices from [0, M) (excluding the index M itself) and the second will contain all indices from [M, N) (excluding the index N itself).

§Panics

Panics if M > N.

§Examples
#![feature(split_array)]

let v = [1, 2, 3, 4, 5, 6];

{
   let (left, right) = v.split_array_ref::<0>();
   assert_eq!(left, &[]);
   assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}

{
    let (left, right) = v.split_array_ref::<2>();
    assert_eq!(left, &[1, 2]);
    assert_eq!(right, &[3, 4, 5, 6]);
}

{
    let (left, right) = v.split_array_ref::<6>();
    assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
    assert_eq!(right, &[]);
}
source

pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])

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

Divides one array reference into two at an index from the end.

The first will contain all indices from [0, N - M) (excluding the index N - M itself) and the second will contain all indices from [N - M, N) (excluding the index N itself).

§Panics

Panics if M > N.

§Examples
#![feature(split_array)]

let v = [1, 2, 3, 4, 5, 6];

{
   let (left, right) = v.rsplit_array_ref::<0>();
   assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
   assert_eq!(right, &[]);
}

{
    let (left, right) = v.rsplit_array_ref::<2>();
    assert_eq!(left, &[1, 2, 3, 4]);
    assert_eq!(right, &[5, 6]);
}

{
    let (left, right) = v.rsplit_array_ref::<6>();
    assert_eq!(left, &[]);
    assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}
source

pub fn as_ascii(&self) -> Option<&[AsciiChar; N]>

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

Converts this array of bytes into a array of ASCII characters, or returns None if any of the characters is non-ASCII.

§Examples
#![feature(ascii_char)]
#![feature(const_option)]

const HEX_DIGITS: [std::ascii::Char; 16] =
    *b"0123456789abcdef".as_ascii().unwrap();

assert_eq!(HEX_DIGITS[1].as_str(), "1");
assert_eq!(HEX_DIGITS[10].as_str(), "a");
source

pub unsafe fn as_ascii_unchecked(&self) -> &[AsciiChar; N]

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

Converts this array of bytes into a array of ASCII characters, without checking whether they’re valid.

§Safety

Every byte in the array must be in 0..=127, or else this is UB.

Trait Implementations§

source§

impl<const N: usize> Clone for StringArray<N>

source§

fn clone(&self) -> StringArray<N>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<const N: usize> Debug for StringArray<N>

source§

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

Formats the value using the given formatter. Read more
source§

impl<const N: usize> Default for StringArray<N>

source§

fn default() -> Self

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

impl<const N: usize> Deref for StringArray<N>

§

type Target = [i8; N]

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<const N: usize> Display for StringArray<N>

source§

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

Formats the value using the given formatter. Read more
source§

impl<const N: usize> From<[i8; N]> for StringArray<N>

source§

fn from(array: [c_char; N]) -> Self

Converts to this type from the input type.
source§

impl<const N: usize> From<StringArray<N>> for [c_char; N]

source§

fn from(array: StringArray<N>) -> Self

Converts to this type from the input type.
source§

impl<const N: usize> Hash for StringArray<N>

source§

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

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl<const N: usize> Ord for StringArray<N>

source§

fn cmp(&self, other: &StringArray<N>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<const N: usize> PartialEq for StringArray<N>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<const N: usize> PartialOrd for StringArray<N>

source§

fn partial_cmp(&self, other: &StringArray<N>) -> Option<Ordering>

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

fn lt(&self, other: &Rhs) -> bool

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

fn le(&self, other: &Rhs) -> bool

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

fn gt(&self, other: &Rhs) -> bool

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

fn ge(&self, other: &Rhs) -> bool

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

impl<const N: usize> Copy for StringArray<N>

source§

impl<const N: usize> Eq for StringArray<N>

Auto Trait Implementations§

§

impl<const N: usize> Freeze for StringArray<N>

§

impl<const N: usize> RefUnwindSafe for StringArray<N>

§

impl<const N: usize> Send for StringArray<N>

§

impl<const N: usize> Sync for StringArray<N>

§

impl<const N: usize> Unpin for StringArray<N>

§

impl<const N: usize> UnwindSafe for StringArray<N>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

source§

fn into(self) -> U

Calls U::from(self).

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

source§

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

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

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

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

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

Performs the conversion.