I am calling Rust code from C code. In the Rust code a &str is constructed from a pointer and a length. This is what I have now:
fn ptr_to_str(ptr: *const u8, len: usize) -> Option<&'static str> {
if ptr.is_null() {
return None;
}
let slice = unsafe { slice::from_raw_parts(ptr, len) };
str::from_utf8(slice).ok()
}
In this case it does not really matter that much, but I don't really like using the static lifetime. Is there a better alternative?