// example 1
extern "C" {
pub fn c_function(arg: *const c_char) -> *const c_char;
}
...
let arg = CString::new("value").unwrap();
let c_buf = unsafe { c_function(arg) };
let c_str: &CStr = unsafe { CStr::from_ptr(c_buf) };
let buf: &[u8] = c_str.to_bytes();
println!("{}", str::from_utf8(buf).unwrap());
// example 2
extern "C" {
pub fn c_function(arg: *const c_char) -> Option<*const c_char>;
}
...
let arg = CString::new("value").unwrap();
let is_null_value = CString::new("null").unwrap();
let c_buf = unsafe { c_function(arg) };
let c_str: &CStr = unsafe { CStr::from_ptr(c_buf.unwrap_or(is_null_value.as_ptr())) };
let buf: &[u8] = c_str.to_bytes();
println!("{}", str::from_utf8(buf).unwrap());
Ly8gZXhhbXBsZSAxCmV4dGVybiAiQyIgewogICAgcHViIGZuIGNfZnVuY3Rpb24oYXJnOiAqY29uc3QgY19jaGFyKSAtPiAqY29uc3QgY19jaGFyOwp9CgouLi4KCmxldCBhcmcgPSBDU3RyaW5nOjpuZXcoInZhbHVlIikudW53cmFwKCk7CmxldCBjX2J1ZiA9IHVuc2FmZSB7IGNfZnVuY3Rpb24oYXJnKSB9OwoKbGV0IGNfc3RyOiAmQ1N0ciA9IHVuc2FmZSB7IENTdHI6OmZyb21fcHRyKGNfYnVmKSB9OwpsZXQgYnVmOiAmW3U4XSA9IGNfc3RyLnRvX2J5dGVzKCk7CnByaW50bG4hKCJ7fSIsIHN0cjo6ZnJvbV91dGY4KGJ1ZikudW53cmFwKCkpOwoKCi8vIGV4YW1wbGUgMgpleHRlcm4gIkMiIHsKICAgIHB1YiBmbiBjX2Z1bmN0aW9uKGFyZzogKmNvbnN0IGNfY2hhcikgLT4gT3B0aW9uPCpjb25zdCBjX2NoYXI+Owp9CgouLi4KCmxldCBhcmcgPSBDU3RyaW5nOjpuZXcoInZhbHVlIikudW53cmFwKCk7CmxldCBpc19udWxsX3ZhbHVlID0gQ1N0cmluZzo6bmV3KCJudWxsIikudW53cmFwKCk7CmxldCBjX2J1ZiA9IHVuc2FmZSB7IGNfZnVuY3Rpb24oYXJnKSB9OwoKbGV0IGNfc3RyOiAmQ1N0ciA9IHVuc2FmZSB7IENTdHI6OmZyb21fcHRyKGNfYnVmLnVud3JhcF9vcihpc19udWxsX3ZhbHVlLmFzX3B0cigpKSkgfTsKbGV0IGJ1ZjogJlt1OF0gPSBjX3N0ci50b19ieXRlcygpOwpwcmludGxuISgie30iLCBzdHI6OmZyb21fdXRmOChidWYpLnVud3JhcCgpKTsK