Skip to content

Instantly share code, notes, and snippets.

@vmlinz
Created May 8, 2015 01:08
Show Gist options
  • Save vmlinz/3b8b7ffc504c00a0a396 to your computer and use it in GitHub Desktop.
Save vmlinz/3b8b7ffc504c00a0a396 to your computer and use it in GitHub Desktop.
Rust and C string interoperation
#![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