Crate const_it

source ·
Expand description

§const it!

This crate provides some utilities for use in const evaluation contexts, in particular const slicing 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 split_slice_at! and try_split_slice_at! macros split a slice in two:

const STR: (&str, &str) = split_slice_at!("const slice", 5); // ("const", " slice")

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§

  • Takes a Result and returns the unwrapped Ok value, or panics if it’s Err. The second argument is the message to use on panic. If the panic message is omitted, the Err value must be of type &str and is used as the panic message.
  • Takes an Option and returns the unwrapped Some value, or panics if it’s None. The second argument is the message to use on panic.
  • Turn a Result into an Option.
  • 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.
  • Split a slice in two at the specified index. Panics on error.
  • 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), or None if the index is out of range or, for strings, if the slice would split a unicode codepoint.
  • Split a slice in two at the specified index. Returns None on error.
  • Takes a Result and returns the unwrapped Ok value, or panics if it’s Err.
  • Takes a Result and evaluates to the unwrapped Ok value, or if it’s Err, returns the Err to the current function’s caller.
  • Takes an Option and returns the unwrapped Some value, or panics if it’s None.
  • Takes an Option and evaluates to the unwrapped Some value, or if it’s None, returns the None to the current function’s caller.

Structs§

  • A pending slice operation. This can be used to slice &[T] and &str in a const context with any valid slice index.

Traits§

  • This trait is similar to the SliceIndex trait in std/core, but it’s implemented for array types too.