str_array
Provides fixed-size string types StrArray<N> and CStrArray<N>.
StrArray serves as the str equivalent of [u8; N].
It provides a Deref to &str and ensures the UTF-8 invariant is
always upheld, but has a size known at compile time.
This is useful in some situations:
- Resource-constrained
no_stdand no-allocenvironments. - Defining UTF-8 strings directly in a stack array or static.
- Types or parameters that require
&strof some fixed length.
The str_array! macro provides a compile-time-checked way to
build StrArray values from string literals and constants.
Similarly, CStrArray and cstr_array! can construct a
nul-terminated CStr safely on the stack.
Features
no_stdsupport - disable default features to use withoutstd- Optional
allocandstdfeatures - Full
constsupport - C string support
Examples
use ;
// Create from a constant using the macro. The length is inferred.
let s1 = str_array!;
assert_eq!;
assert_eq!;
assert!;
// Or create from a runtime &str with an length check.
let s2: = new.unwrap;
assert_eq!;
assert_eq!;
// Or create from bytes with a UTF-8 check.
let s3 = from_utf8.unwrap;
assert_eq!;
// Or define an item with an inferred length.
str_array!
assert_eq!;
Rust feature detection
This crate has a low Minimum Supported Rust Version (MSRV), and it achieves
that through cfg values that are enabled in the build.rs based on the
presence of existing features. This uses the autocfg library.
The build.rs can be skipped for alternate build systems, but be sure to
enable the appropriate cfg values based on your version of rustc.
Those are:
cfg(has_core_error)is enabled whencore::error::Erroris present (stable since Rust 1.81). If it's enabled, then the error types in this crate implementErrorwith any set of features. If it's disabled, they only implement theErrortrait when thestdfeature is enabled.cfg(has_const_mut)is enabled when&mutis usable inconst(stable since Rust 1.83). It addsconstto variousfnthat use&mutin this crate.
License
Licensed under either of the Apache 2.0 or MIT licenses.