Skip to content

Instantly share code, notes, and snippets.

@robertohuertasm
Created November 3, 2019 10:43
Show Gist options
  • Save robertohuertasm/89f66bc45f6a655575df5fb8a23273cc to your computer and use it in GitHub Desktop.
Save robertohuertasm/89f66bc45f6a655575df5fb8a23273cc to your computer and use it in GitHub Desktop.
rust_for_android_ios_flutter
use std::ffi::{CStr, CString};
use std::os::raw::c_char;
#[no_mangle]
pub unsafe extern "C" fn hello(to: *const c_char) -> *mut c_char {
let c_str = CStr::from_ptr(to);
let recipient = match c_str.to_str() {
Ok(s) => s,
Err(_) => "you",
};
CString::new(format!("Hello from Rust: {}", recipient))
.unwrap()
.into_raw()
}
#[no_mangle]
pub unsafe extern "C" fn hello_release(s: *mut c_char) {
if s.is_null() {
return;
}
CString::from_raw(s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment