Expand description
§const it!
This crate provides some utilities for use in const evaluation contexts, in particular const slice and error handling.
The slice! and try_slice! macros slice (using any usize or range expression):
const STR: &str = slice!("const slice", ..5); // "const"The slice_split_at! and slice_try_split_at! macros split a slice in two:
const STR: (&str, &str) = slice_split_at!("const slice", 5); // ("const", " slice")The slice_cmp! and slice_eq! macros compare slices. slice_starts_with! and
slice_strip_prefix! checks for and strips a prefix, respectively, and
slice_ends_with! and slice_strip_suffix! do the same for suffixes.
The ok!, expect_ok!, unwrap_ok!, unwrap_ok_or_return!, expect_some!, unwrap_some!
and unwrap_some_or_return! macros work with Results and Options.
Macros§
- expect_
ok - Takes a
Resultand returns the unwrappedOkvalue, or panics if it’sErr. The second argument is the message to use on panic. If the panic message is omitted, theErrvalue must be of type&strand is used as the panic message. - expect_
some - Takes an
Optionand returns the unwrappedSomevalue, or panics if it’sNone. The second argument is the message to use on panic. - ok
- Turn a
Resultinto anOption. - slice
- Slice an item in a const context. The first argument is the item to slice, and the second is the slice index, which can be a usize or any usize range type. Panics if the index is out of range or, for strings, if the slice would split a unicode codepoint.
- slice_
cmp - Compare two slices, returning an
Ordering. This only works for slices of primitive integer types andstr. - slice_
ends_ with - Check if a slice ends with another slice. This only works for slices of primitive integer types and
str. - slice_
eq - Check if two slices are equal. This only works for slices of primitive integer types and
str. - slice_
partial_ cmp - Compare two slices, returning an
Option<Ordering>. Currently all supported types always returnSome. This only works for slices of primitive integer types andstr. - slice_
split_ at - Split a slice in two at the specified index. Panics on error.
- slice_
starts_ with - Check if a slice starts with another slice. This only works for slices of primitive integer types and
str. - slice_
strip_ prefix - Strip a prefix from a slice, returning an Option with the stripped slice on success. This only works for slices of primitive integer types and
str. - slice_
strip_ suffix - Strip a suffix from a slice, returning an Option with the stripped slice on success. This only works for slices of primitive integer types and
str. - slice_
try_ split_ at - Split a slice in two at the specified index. Returns
Noneon error. - try_
slice - Slice an item in a const context. The first argument is the item to slice, and
the second is the slice index, which can be a usize or any usize range type.
Returns
Some(sliced), orNoneif the index is out of range or, for strings, if the slice would split a unicode codepoint. - unwrap_
ok - Takes a
Resultand returns the unwrappedOkvalue, or panics if it’sErr. - unwrap_
ok_ or_ return - Takes a
Resultand evaluates to the unwrappedOkvalue, or if it’sErr, returns theErrto the current function’s caller. - unwrap_
some - Takes an
Optionand returns the unwrappedSomevalue, or panics if it’sNone. - unwrap_
some_ or_ return - Takes an
Optionand evaluates to the unwrappedSomevalue, or if it’sNone, returns theNoneto the current function’s caller.