raw_cstr

Function raw_cstr 

Source
pub fn raw_cstr<S>(str: S) -> Result<*mut i8>
where S: AsRef<str>,
Expand description

Create a constant raw C string as a *mut i8 from a Rust string reference. C Strings are cached, and creating the same string twice will cost zero additional memory. This is useful when calling C APIs that take a string as an argument, particularly when the string can’t be known at compile time, although this function is also efficient in space (but not time) when a constant string is known. For compile-time constants, you can use c_str!.

§Safety

  • Do not use String::from_raw_parts to convert the pointer back to a String. This may cause a double free because the String will take ownership of the pointer. Use CStr::from_ptr instead, and convert to a string with .to_str().expect("...").to_owned() instead.