Skip to content

Instantly share code, notes, and snippets.

@felix-d
Created July 9, 2017 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felix-d/acba9c8626e920f74a71e50eda74aca9 to your computer and use it in GitHub Desktop.
Save felix-d/acba9c8626e920f74a71e50eda74aca9 to your computer and use it in GitHub Desktop.
FFI - Convert C strings to Rust strings
extern crate libc;
use std::ffi::CStr;
use libc::c_char;
pub extern fn to_owned(buf: *const c_char) -> String {
let c_str: &CStr = unsafe { CStr::from_ptr(buf) };
let buf: &[u8] = c_str.to_bytes();
let str_slice: &str = std::str::from_utf8(buf).unwrap();
str_slice.to_owned()
}
@felix-d
Copy link
Author

felix-d commented Jul 9, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment