fork download
  1. // example 1
  2. extern "C" {
  3. pub fn c_function(arg: *const c_char) -> *const c_char;
  4. }
  5.  
  6. ...
  7.  
  8. let arg = CString::new("value").unwrap();
  9. let c_buf = unsafe { c_function(arg) };
  10.  
  11. let c_str: &CStr = unsafe { CStr::from_ptr(c_buf) };
  12. let buf: &[u8] = c_str.to_bytes();
  13. println!("{}", str::from_utf8(buf).unwrap());
  14.  
  15.  
  16. // example 2
  17. extern "C" {
  18. pub fn c_function(arg: *const c_char) -> Option<*const c_char>;
  19. }
  20.  
  21. ...
  22.  
  23. let arg = CString::new("value").unwrap();
  24. let is_null_value = CString::new("null").unwrap();
  25. let c_buf = unsafe { c_function(arg) };
  26.  
  27. let c_str: &CStr = unsafe { CStr::from_ptr(c_buf.unwrap_or(is_null_value.as_ptr())) };
  28. let buf: &[u8] = c_str.to_bytes();
  29. println!("{}", str::from_utf8(buf).unwrap());
  30.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.rs:6:1: 6:4 error: expected item, found `...`
prog.rs:6 ...
          ^~~
stdout
Standard output is empty