Skip to content

Instantly share code, notes, and snippets.

@ayazhafiz
Last active March 1, 2018 01:49
Show Gist options
  • Save ayazhafiz/d90ff4e2920ba59aa562d870f5042913 to your computer and use it in GitHub Desktop.
Save ayazhafiz/d90ff4e2920ba59aa562d870f5042913 to your computer and use it in GitHub Desktop.
extern crate libc;
use std::ffi::{CStr, CString};
use std::str;
use libc::c_char;
#[no_mangle]
pub extern "C" fn hello(raw_name: *const c_char) -> *const c_char {
// convert to Rust String
let name = str::from_utf8(
unsafe { CStr::from_ptr(raw_name).to_bytes() }
).unwrap();
// format and return readable char pointer
let formatted = format!("Hello, {}!", name);
CString::new(formatted).unwrap().into_raw()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment