Created
May 8, 2015 01:08
-
-
Save vmlinz/3b8b7ffc504c00a0a396 to your computer and use it in GitHub Desktop.
Rust and C string interoperation
This file contains 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
#![feature(libc)] | |
extern crate libc; | |
use std::ffi::{CStr, CString}; | |
use std::str; | |
use libc::*; | |
extern "C" fn my_string() -> *const c_char { | |
let c_to_print = CString::new("Hello").unwrap(); | |
c_to_print.as_ptr() | |
} | |
fn main() { | |
unsafe { | |
let slice = CStr::from_ptr(my_string()); | |
println!("string returned: {}" , str:: from_utf8 ( | |
slice . to_bytes ( ) ) . unwrap ( )); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment