konst::ffi::cstr

Function from_bytes_with_nul

Source
pub const fn from_bytes_with_nul(
    bytes: &[u8],
) -> Result<&CStr, FromBytesWithNulError>
Expand description

Converts a nul-terminated byte slice into a &CStr. Const equivalent of CStr::from_bytes_with_nul

§Const stabilization

The equivalent std function was const-stabilized in Rust 1.72.0.

§Example

use konst::{ffi::cstr, unwrap_ctx};

use std::ffi::CStr;


const CS: &CStr = unwrap_ctx!(cstr::from_bytes_with_nul(b"foo bar\0"));

assert_eq!(CS.to_str().unwrap(), "foo bar");