Created
November 3, 2019 10:43
-
-
Save robertohuertasm/89f66bc45f6a655575df5fb8a23273cc to your computer and use it in GitHub Desktop.
rust_for_android_ios_flutter
This file contains hidden or 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
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