Created
July 9, 2017 17:43
-
-
Save felix-d/acba9c8626e920f74a71e50eda74aca9 to your computer and use it in GitHub Desktop.
FFI - Convert C strings to Rust strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See this stack overflow answer for more details