Expand description
A fixed-size array, denoted [T; N]
, for the element type, T
, and the
non-negative compile-time constant size, N
.
There are two syntactic forms for creating an array:
- A list with each element, i.e.,
[x, y, z]
. - A repeat expression
[x; N]
, which produces an array withN
copies ofx
. The type ofx
must beCopy
.
Note that [expr; 0]
is allowed, and produces an empty array.
This will still evaluate expr
, however, and immediately drop the resulting value, so
be mindful of side effects.
Arrays of any size implement the following traits if the element type allows it:
Copy
Clone
Debug
IntoIterator
(implemented for[T; N]
,&[T; N]
and&mut [T; N]
)PartialEq
,PartialOrd
,Eq
,Ord
Hash
AsRef
,AsMut
Borrow
,BorrowMut
Arrays of sizes from 0 to 32 (inclusive) implement the Default
trait
if the element type allows it. As a stopgap, trait implementations are
statically generated up to size 32.
Arrays coerce to slices ([T]
), so a slice method may be called on
an array. Indeed, this provides most of the API for working with arrays.
Slices have a dynamic size and do not coerce to arrays. Instead, use
slice.try_into().unwrap()
or <ArrayType>::try_from(slice).unwrap()
.
Array’s try_from(slice)
implementations (and the corresponding slice.try_into()
array implementations) succeed if the input slice length is the same as the result
array length. They optimize especially well when the optimizer can easily determine
the slice length, e.g. <[u8; 4]>::try_from(&slice[4..8]).unwrap()
. Array implements
TryFrom returning:
[T; N]
copies from the slice’s elements&[T; N]
references the original slice’s elements&mut [T; N]
references the original slice’s elements
You can move elements out of an array with a slice pattern. If you want
one element, see mem::replace
.
Examples
let mut array: [i32; 3] = [0; 3];
array[1] = 1;
array[2] = 2;
assert_eq!([1, 2], &array[1..]);
// This loop prints: 0 1 2
for x in array {
print!("{x} ");
}
RunYou can also iterate over reference to the array’s elements:
let array: [i32; 3] = [0; 3];
for x in &array { }
RunYou can use <ArrayType>::try_from(slice)
or slice.try_into()
to get an array from
a slice:
let bytes: [u8; 3] = [1, 0, 2];
assert_eq!(1, u16::from_le_bytes(<[u8; 2]>::try_from(&bytes[0..2]).unwrap()));
assert_eq!(512, u16::from_le_bytes(bytes[1..3].try_into().unwrap()));
RunYou can use a slice pattern to move elements out of an array:
fn move_away(_: String) { /* Do interesting things. */ }
let [john, roa] = ["John".to_string(), "Roa".to_string()];
move_away(john);
move_away(roa);
RunEditions
Prior to Rust 1.53, arrays did not implement IntoIterator
by value, so the method call
array.into_iter()
auto-referenced into a slice iterator. Right now, the old
behavior is preserved in the 2015 and 2018 editions of Rust for compatibility, ignoring
IntoIterator
by value. In the future, the behavior on the 2015 and 2018 edition
might be made consistent to the behavior of later editions.
// Rust 2015 and 2018:
let array: [i32; 3] = [0; 3];
// This creates a slice iterator, producing references to each value.
for item in array.into_iter().enumerate() {
let (i, x): (usize, &i32) = item;
println!("array[{i}] = {x}");
}
// The `array_into_iter` lint suggests this change for future compatibility:
for item in array.iter().enumerate() {
let (i, x): (usize, &i32) = item;
println!("array[{i}] = {x}");
}
// You can explicitly iterate an array by value using `IntoIterator::into_iter`
for item in IntoIterator::into_iter(array).enumerate() {
let (i, x): (usize, i32) = item;
println!("array[{i}] = {x}");
}
RunStarting in the 2021 edition, array.into_iter()
uses IntoIterator
normally to iterate
by value, and iter()
should be used to iterate by reference like previous editions.
// Rust 2021:
let array: [i32; 3] = [0; 3];
// This iterates by reference:
for item in array.iter().enumerate() {
let (i, x): (usize, &i32) = item;
println!("array[{i}] = {x}");
}
// This iterates by value:
for item in array.into_iter().enumerate() {
let (i, x): (usize, i32) = item;
println!("array[{i}] = {x}");
}
RunFuture language versions might start treating the array.into_iter()
syntax on editions 2015 and 2018 the same as on edition 2021. So code using
those older editions should still be written with this change in mind, to
prevent breakage in the future. The safest way to accomplish this is to
avoid the into_iter
syntax on those editions. If an edition update is not
viable/desired, there are multiple alternatives:
- use
iter
, equivalent to the old behavior, creating references - use
IntoIterator::into_iter
, equivalent to the post-2021 behavior (Rust 1.53+) - replace
for ... in array.into_iter() {
withfor ... in array {
, equivalent to the post-2021 behavior (Rust 1.53+)
// Rust 2015 and 2018:
let array: [i32; 3] = [0; 3];
// This iterates by reference:
for item in array.iter() {
let x: &i32 = item;
println!("{x}");
}
// This iterates by value:
for item in IntoIterator::into_iter(array) {
let x: i32 = item;
println!("{x}");
}
// This iterates by value:
for item in array {
let x: i32 = item;
println!("{x}");
}
// IntoIter can also start a chain.
// This iterates by value:
for item in IntoIterator::into_iter(array).enumerate() {
let (i, x): (usize, i32) = item;
println!("array[{i}] = {x}");
}
RunImplementations
sourceimpl<T, const N: usize> [MaybeUninit<T>; N]
impl<T, const N: usize> [MaybeUninit<T>; N]
sourcepub const fn transpose(self) -> MaybeUninit<[T; N]>
🔬This is a nightly-only experimental API. (maybe_uninit_uninit_array_transpose
#96097)
pub const fn transpose(self) -> MaybeUninit<[T; N]>
maybe_uninit_uninit_array_transpose
#96097)sourceimpl<T, const N: usize> [T; N]
impl<T, const N: usize> [T; N]
1.55.0 · sourcepub fn map<F, U>(self, f: F) -> [U; N]where
F: FnMut(T) -> U,
pub fn map<F, U>(self, f: F) -> [U; N]where
F: FnMut(T) -> U,
Returns an array of the same size as self
, with function f
applied to each element
in order.
If you don’t necessarily need a new fixed-size array, consider using
Iterator::map
instead.
Note on performance and stack usage
Unfortunately, usages of this method are currently not always optimized as well as they could be. This mainly concerns large arrays, as mapping over small arrays seem to be optimized just fine. Also note that in debug mode (i.e. without any optimizations), this method can use a lot of stack space (a few times the size of the array or more).
Therefore, in performance-critical code, try to avoid using this method
on large arrays or check the emitted code. Also try to avoid chained
maps (e.g. arr.map(...).map(...)
).
In many cases, you can instead use Iterator::map
by calling .iter()
or .into_iter()
on your array. [T; N]::map
is only necessary if you
really need a new array of the same size as the result. Rust’s lazy
iterators tend to get optimized very well.
Examples
let x = [1, 2, 3];
let y = x.map(|v| v + 1);
assert_eq!(y, [2, 3, 4]);
let x = [1, 2, 3];
let mut temp = 0;
let y = x.map(|v| { temp += 1; v * temp });
assert_eq!(y, [1, 4, 9]);
let x = ["Ferris", "Bueller's", "Day", "Off"];
let y = x.map(|v| v.len());
assert_eq!(y, [6, 9, 3, 3]);
Runsourcepub fn try_map<F, R>(
self,
f: F
) -> <<R as Try>::Residual as Residual<[R::Output; N]>>::TryTypewhere
F: FnMut(T) -> R,
R: Try,
R::Residual: Residual<[R::Output; N]>,
🔬This is a nightly-only experimental API. (array_try_map
#79711)
pub fn try_map<F, R>(
self,
f: F
) -> <<R as Try>::Residual as Residual<[R::Output; N]>>::TryTypewhere
F: FnMut(T) -> R,
R: Try,
R::Residual: Residual<[R::Output; N]>,
array_try_map
#79711)A fallible function f
applied to each element on array self
in order to
return an array the same size as self
or the first error encountered.
The return type of this function depends on the return type of the closure.
If you return Result<T, E>
from the closure, you’ll get a Result<[T; N]; E>
.
If you return Option<T>
from the closure, you’ll get an Option<[T; N]>
.
Examples
#![feature(array_try_map)]
let a = ["1", "2", "3"];
let b = a.try_map(|v| v.parse::<u32>()).unwrap().map(|v| v + 1);
assert_eq!(b, [2, 3, 4]);
let a = ["1", "2a", "3"];
let b = a.try_map(|v| v.parse::<u32>());
assert!(b.is_err());
use std::num::NonZeroU32;
let z = [1, 2, 0, 3, 4];
assert_eq!(z.try_map(NonZeroU32::new), None);
let a = [1, 2, 3];
let b = a.try_map(NonZeroU32::new);
let c = b.map(|x| x.map(NonZeroU32::get));
assert_eq!(c, Some(a));
Runsourcepub fn zip<U>(self, rhs: [U; N]) -> [(T, U); N]
🔬This is a nightly-only experimental API. (array_zip
#80094)
pub fn zip<U>(self, rhs: [U; N]) -> [(T, U); N]
array_zip
#80094)‘Zips up’ two arrays into a single array of pairs.
zip()
returns a new array where every element is a tuple where the
first element comes from the first array, and the second element comes
from the second array. In other words, it zips two arrays together,
into a single one.
Examples
#![feature(array_zip)]
let x = [1, 2, 3];
let y = [4, 5, 6];
let z = x.zip(y);
assert_eq!(z, [(1, 4), (2, 5), (3, 6)]);
Run1.57.0 (const: 1.57.0) · sourcepub const fn as_slice(&self) -> &[T]
pub const fn as_slice(&self) -> &[T]
Returns a slice containing the entire array. Equivalent to &s[..]
.
1.57.0 · sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
Returns a mutable slice containing the entire array. Equivalent to
&mut s[..]
.
sourcepub fn each_ref(&self) -> [&T; N]
🔬This is a nightly-only experimental API. (array_methods
#76118)
pub fn each_ref(&self) -> [&T; N]
array_methods
#76118)Borrows each element and returns an array of references with the same
size as self
.
Example
#![feature(array_methods)]
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]);
RunThis 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
.
#![feature(array_methods)]
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);
Runsourcepub fn each_mut(&mut self) -> [&mut T; N]
🔬This is a nightly-only experimental API. (array_methods
#76118)
pub fn each_mut(&mut self) -> [&mut T; N]
array_methods
#76118)Borrows each element mutably and returns an array of mutable references
with the same size as self
.
Example
#![feature(array_methods)]
let mut floats = [3.1, 2.7, -1.0];
let float_refs: [&mut f64; 3] = floats.each_mut();
*float_refs[0] = 0.0;
assert_eq!(float_refs, [&mut 0.0, &mut 2.7, &mut -1.0]);
assert_eq!(floats, [0.0, 2.7, -1.0]);
Runsourcepub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])
🔬This is a nightly-only experimental API. (split_array
#90091)
pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])
split_array
#90091)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, &[]);
}
Runsourcepub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T])
🔬This is a nightly-only experimental API. (split_array
#90091)
pub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T])
split_array
#90091)Divides one mutable 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 mut v = [1, 0, 3, 0, 5, 6];
let (left, right) = v.split_array_mut::<2>();
assert_eq!(left, &mut [1, 0][..]);
assert_eq!(right, &mut [3, 0, 5, 6]);
left[1] = 2;
right[1] = 4;
assert_eq!(v, [1, 2, 3, 4, 5, 6]);
Runsourcepub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])
🔬This is a nightly-only experimental API. (split_array
#90091)
pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])
split_array
#90091)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]);
}
Runsourcepub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M])
🔬This is a nightly-only experimental API. (split_array
#90091)
pub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M])
split_array
#90091)Divides one mutable 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 mut v = [1, 0, 3, 0, 5, 6];
let (left, right) = v.rsplit_array_mut::<4>();
assert_eq!(left, &mut [1, 0]);
assert_eq!(right, &mut [3, 0, 5, 6][..]);
left[1] = 2;
right[1] = 4;
assert_eq!(v, [1, 2, 3, 4, 5, 6]);
RunTrait Implementations
sourceimpl<T, const LANES: usize> AsMut<[T; LANES]> for Simd<T, LANES>where
LaneCount<LANES>: SupportedLaneCount,
T: SimdElement,
impl<T, const LANES: usize> AsMut<[T; LANES]> for Simd<T, LANES>where
LaneCount<LANES>: SupportedLaneCount,
T: SimdElement,
sourcefn as_mut(&mut self) -> &mut [T; LANES]
fn as_mut(&mut self) -> &mut [T; LANES]
sourceimpl<T, const LANES: usize> AsRef<[T; LANES]> for Simd<T, LANES>where
LaneCount<LANES>: SupportedLaneCount,
T: SimdElement,
impl<T, const LANES: usize> AsRef<[T; LANES]> for Simd<T, LANES>where
LaneCount<LANES>: SupportedLaneCount,
T: SimdElement,
sourcefn as_ref(&self) -> &[T; LANES]
fn as_ref(&self) -> &[T; LANES]
1.4.0 (const: unstable) · sourceimpl<T, const N: usize> BorrowMut<[T]> for [T; N]
impl<T, const N: usize> BorrowMut<[T]> for [T; N]
const: unstable · sourcefn borrow_mut(&mut self) -> &mut [T]
fn borrow_mut(&mut self) -> &mut [T]
sourceimpl<T, const LANES: usize> From<[T; LANES]> for Simd<T, LANES>where
LaneCount<LANES>: SupportedLaneCount,
T: SimdElement,
impl<T, const LANES: usize> From<[T; LANES]> for Simd<T, LANES>where
LaneCount<LANES>: SupportedLaneCount,
T: SimdElement,
sourcefn from(array: [T; LANES]) -> Self
fn from(array: [T; LANES]) -> Self
sourceimpl<T, const LANES: usize> From<[bool; LANES]> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> From<[bool; LANES]> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
sourceimpl<T, const LANES: usize> From<Mask<T, LANES>> for [bool; LANES]where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> From<Mask<T, LANES>> for [bool; LANES]where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
sourceimpl<T, const LANES: usize> From<Simd<T, LANES>> for [T; LANES]where
LaneCount<LANES>: SupportedLaneCount,
T: SimdElement,
impl<T, const LANES: usize> From<Simd<T, LANES>> for [T; LANES]where
LaneCount<LANES>: SupportedLaneCount,
T: SimdElement,
sourceimpl<T: Hash, const N: usize> Hash for [T; N]
impl<T: Hash, const N: usize> Hash for [T; N]
The hash of an array is the same as that of the corresponding slice,
as required by the Borrow
implementation.
#![feature(build_hasher_simple_hash_one)]
use std::hash::BuildHasher;
let b = std::collections::hash_map::RandomState::new();
let a: [u8; 3] = [0xa8, 0x3c, 0x09];
let s: &[u8] = &[0xa8, 0x3c, 0x09];
assert_eq!(b.hash_one(a), b.hash_one(s));
Run1.50.0 (const: unstable) · sourceimpl<T, I, const N: usize> IndexMut<I> for [T; N]where
[T]: IndexMut<I>,
impl<T, I, const N: usize> IndexMut<I> for [T; N]where
[T]: IndexMut<I>,
sourceimpl<'a, T, const N: usize> IntoIterator for &'a [T; N]
impl<'a, T, const N: usize> IntoIterator for &'a [T; N]
sourceimpl<'a, T, const N: usize> IntoIterator for &'a mut [T; N]
impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N]
1.53.0 · sourceimpl<T, const N: usize> IntoIterator for [T; N]
impl<T, const N: usize> IntoIterator for [T; N]
sourcefn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Creates a consuming iterator, that is, one that moves each value out of
the array (from start to end). The array cannot be used after calling
this unless T
implements Copy
, so the whole array is copied.
Arrays have special behavior when calling .into_iter()
prior to the
2021 edition – see the array Editions section for more information.
type Item = T
type Item = T
sourceimpl<T: Ord, const N: usize> Ord for [T; N]
impl<T: Ord, const N: usize> Ord for [T; N]
Implements comparison of arrays lexicographically.
1.21.0 · sourcefn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 · sourcefn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: Sized,
Self: PartialOrd,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: Sized,
Self: PartialOrd,
sourceimpl<A, B, const N: usize> PartialEq<&[B]> for [A; N]where
A: PartialEq<B>,
impl<A, B, const N: usize> PartialEq<&[B]> for [A; N]where
A: PartialEq<B>,
sourceimpl<A, B, const N: usize> PartialEq<&mut [B]> for [A; N]where
A: PartialEq<B>,
impl<A, B, const N: usize> PartialEq<&mut [B]> for [A; N]where
A: PartialEq<B>,
sourceimpl<A, B, const N: usize> PartialEq<[A; N]> for &[B]where
B: PartialEq<A>,
impl<A, B, const N: usize> PartialEq<[A; N]> for &[B]where
B: PartialEq<A>,
sourceimpl<A, B, const N: usize> PartialEq<[A; N]> for &mut [B]where
B: PartialEq<A>,
impl<A, B, const N: usize> PartialEq<[A; N]> for &mut [B]where
B: PartialEq<A>,
sourceimpl<A, B, const N: usize> PartialEq<[A; N]> for [B]where
B: PartialEq<A>,
impl<A, B, const N: usize> PartialEq<[A; N]> for [B]where
B: PartialEq<A>,
sourceimpl<A, B, const N: usize> PartialEq<[B; N]> for [A; N]where
A: PartialEq<B>,
impl<A, B, const N: usize> PartialEq<[B; N]> for [A; N]where
A: PartialEq<B>,
sourceimpl<A, B, const N: usize> PartialEq<[B]> for [A; N]where
A: PartialEq<B>,
impl<A, B, const N: usize> PartialEq<[B]> for [A; N]where
A: PartialEq<B>,
sourceimpl<T: PartialOrd, const N: usize> PartialOrd<[T; N]> for [T; N]
impl<T: PartialOrd, const N: usize> PartialOrd<[T; N]> for [T; N]
sourcefn partial_cmp(&self, other: &[T; N]) -> Option<Ordering>
fn partial_cmp(&self, other: &[T; N]) -> Option<Ordering>
sourcefn le(&self, other: &[T; N]) -> bool
fn le(&self, other: &[T; N]) -> bool
self
and other
) and is used by the <=
operator. Read moresourceimpl<'a, 'b, const N: usize> Pattern<'a> for &'b [char; N]
impl<'a, 'b, const N: usize> Pattern<'a> for &'b [char; N]
Searches for chars that are equal to any of the char
s in the array.
Examples
assert_eq!("Hello world".find(&['l', 'l']), Some(2));
assert_eq!("Hello world".find(&['l', 'l']), Some(2));
Runtype Searcher = CharArrayRefSearcher<'a, 'b, N>
type Searcher = CharArrayRefSearcher<'a, 'b, N>
pattern
#27721)sourcefn into_searcher(self, haystack: &'a str) -> CharArrayRefSearcher<'a, 'b, N>
fn into_searcher(self, haystack: &'a str) -> CharArrayRefSearcher<'a, 'b, N>
pattern
#27721)sourcefn is_contained_in(self, haystack: &'a str) -> bool
fn is_contained_in(self, haystack: &'a str) -> bool
pattern
#27721)sourcefn is_prefix_of(self, haystack: &'a str) -> bool
fn is_prefix_of(self, haystack: &'a str) -> bool
pattern
#27721)sourcefn strip_prefix_of(self, haystack: &'a str) -> Option<&'a str>
fn strip_prefix_of(self, haystack: &'a str) -> Option<&'a str>
pattern
#27721)sourcefn is_suffix_of(self, haystack: &'a str) -> boolwhere
CharArrayRefSearcher<'a, 'b, N>: ReverseSearcher<'a>,
fn is_suffix_of(self, haystack: &'a str) -> boolwhere
CharArrayRefSearcher<'a, 'b, N>: ReverseSearcher<'a>,
pattern
#27721)sourcefn strip_suffix_of(self, haystack: &'a str) -> Option<&'a str>where
CharArrayRefSearcher<'a, 'b, N>: ReverseSearcher<'a>,
fn strip_suffix_of(self, haystack: &'a str) -> Option<&'a str>where
CharArrayRefSearcher<'a, 'b, N>: ReverseSearcher<'a>,
pattern
#27721)sourceimpl<'a, const N: usize> Pattern<'a> for [char; N]
impl<'a, const N: usize> Pattern<'a> for [char; N]
Searches for chars that are equal to any of the char
s in the array.
Examples
assert_eq!("Hello world".find(['l', 'l']), Some(2));
assert_eq!("Hello world".find(['l', 'l']), Some(2));
Runtype Searcher = CharArraySearcher<'a, N>
type Searcher = CharArraySearcher<'a, N>
pattern
#27721)sourcefn into_searcher(self, haystack: &'a str) -> CharArraySearcher<'a, N>
fn into_searcher(self, haystack: &'a str) -> CharArraySearcher<'a, N>
pattern
#27721)sourcefn is_contained_in(self, haystack: &'a str) -> bool
fn is_contained_in(self, haystack: &'a str) -> bool
pattern
#27721)sourcefn is_prefix_of(self, haystack: &'a str) -> bool
fn is_prefix_of(self, haystack: &'a str) -> bool
pattern
#27721)sourcefn strip_prefix_of(self, haystack: &'a str) -> Option<&'a str>
fn strip_prefix_of(self, haystack: &'a str) -> Option<&'a str>
pattern
#27721)sourcefn is_suffix_of(self, haystack: &'a str) -> boolwhere
CharArraySearcher<'a, N>: ReverseSearcher<'a>,
fn is_suffix_of(self, haystack: &'a str) -> boolwhere
CharArraySearcher<'a, N>: ReverseSearcher<'a>,
pattern
#27721)sourcefn strip_suffix_of(self, haystack: &'a str) -> Option<&'a str>where
CharArraySearcher<'a, N>: ReverseSearcher<'a>,
fn strip_suffix_of(self, haystack: &'a str) -> Option<&'a str>where
CharArraySearcher<'a, N>: ReverseSearcher<'a>,
pattern
#27721)1.51.0 · sourceimpl<T, const N: usize> SlicePattern for [T; N]
impl<T, const N: usize> SlicePattern for [T; N]
1.34.0 · sourceimpl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N]
impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N]
Tries to create an array ref &[T; N]
from a slice ref &[T]
. Succeeds if
slice.len() == N
.
let bytes: [u8; 3] = [1, 0, 2];
let bytes_head: &[u8; 2] = <&[u8; 2]>::try_from(&bytes[0..2]).unwrap();
assert_eq!(1, u16::from_le_bytes(*bytes_head));
let bytes_tail: &[u8; 2] = bytes[1..3].try_into().unwrap();
assert_eq!(512, u16::from_le_bytes(*bytes_tail));
Runtype Error = TryFromSliceError
type Error = TryFromSliceError
1.34.0 · sourceimpl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N]
impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N]
Tries to create a mutable array ref &mut [T; N]
from a mutable slice ref
&mut [T]
. Succeeds if slice.len() == N
.
let mut bytes: [u8; 3] = [1, 0, 2];
let bytes_head: &mut [u8; 2] = <&mut [u8; 2]>::try_from(&mut bytes[0..2]).unwrap();
assert_eq!(1, u16::from_le_bytes(*bytes_head));
let bytes_tail: &mut [u8; 2] = (&mut bytes[1..3]).try_into().unwrap();
assert_eq!(512, u16::from_le_bytes(*bytes_tail));
Runtype Error = TryFromSliceError
type Error = TryFromSliceError
1.34.0 · sourceimpl<T, const N: usize> TryFrom<&[T]> for [T; N]where
T: Copy,
impl<T, const N: usize> TryFrom<&[T]> for [T; N]where
T: Copy,
Tries to create an array [T; N]
by copying from a slice &[T]
. Succeeds if
slice.len() == N
.
let bytes: [u8; 3] = [1, 0, 2];
let bytes_head: [u8; 2] = <[u8; 2]>::try_from(&bytes[0..2]).unwrap();
assert_eq!(1, u16::from_le_bytes(bytes_head));
let bytes_tail: [u8; 2] = bytes[1..3].try_into().unwrap();
assert_eq!(512, u16::from_le_bytes(bytes_tail));
Runtype Error = TryFromSliceError
type Error = TryFromSliceError
1.59.0 · sourceimpl<T, const N: usize> TryFrom<&mut [T]> for [T; N]where
T: Copy,
impl<T, const N: usize> TryFrom<&mut [T]> for [T; N]where
T: Copy,
Tries to create an array [T; N]
by copying from a mutable slice &mut [T]
.
Succeeds if slice.len() == N
.
let mut bytes: [u8; 3] = [1, 0, 2];
let bytes_head: [u8; 2] = <[u8; 2]>::try_from(&mut bytes[0..2]).unwrap();
assert_eq!(1, u16::from_le_bytes(bytes_head));
let bytes_tail: [u8; 2] = (&mut bytes[1..3]).try_into().unwrap();
assert_eq!(512, u16::from_le_bytes(bytes_tail));
Run