pub const fn from_utf8(slice: &[u8]) -> Result<&str, Utf8Error>
Expand description
A const equivalent of std::str::from_utf8
,
requires Rust 1.55 and the "rust_1_55"
feature.
For an alternative that works in Rust 1.46.0,
there is the from_utf8
macro,
but it can only be used in const
s, not in const fn
s .
ยงExample
use konst::{string, unwrap_ctx};
const OK: &str = unwrap_ctx!(string::from_utf8(b"hello world"));
assert_eq!(OK, "hello world");
const ERR: Result<&str, string::Utf8Error> = string::from_utf8(&[32, 34, 255]);
assert_eq!(ERR.unwrap_err().valid_up_to(), 2);