Function konst::string::from_utf8

source ·
pub const fn from_utf8(slice: &[u8]) -> Result<&str, Utf8Error>
Expand description

Delegates to core::str::from_utf8, wrapping the error to provide a panic method for use in unwrap_ctx

§Example

§Basic

use konst::{
    result::unwrap_ctx,
    string,
};

const STR: &str = unwrap_ctx!(string::from_utf8(b"foo bar"));

assert_eq!(STR, "foo bar")

§Compile-time error

use konst::{
    result::unwrap_ctx,
    string,
};

const _: &str = unwrap_ctx!(string::from_utf8(&[255, 255, 255]));
error[E0080]: evaluation of constant value failed
 --> src/string.rs:88:17
  |
9 | const _: &str = unwrap_ctx!(string::from_utf8(&[255, 255, 255]));
  |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'invalid utf-8 sequence of 1 bytes from index 0', src/string.rs:9:17
  |
  = note: this error originates in the macro `unwrap_ctx` (in Nightly builds, run with -Z macro-backtrace for more info)