Last active
March 1, 2018 01:49
-
-
Save ayazhafiz/d90ff4e2920ba59aa562d870f5042913 to your computer and use it in GitHub Desktop.
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
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