Skip to content

Instantly share code, notes, and snippets.

@mfpiccolo
Last active August 29, 2015 14:24
Show Gist options
  • Save mfpiccolo/6e2b92af1ef50c12ff22 to your computer and use it in GitHub Desktop.
Save mfpiccolo/6e2b92af1ef50c12ff22 to your computer and use it in GitHub Desktop.
Final ruby reverse string
#![feature(libc)]
#![feature(cstr_to_str)]
#![feature(cstr_memory)]
extern crate libc;
use std::ffi::{CStr, CString};
#[no_mangle]
pub extern fn ruby_reverse(s: *const libc::c_char) -> *const libc::c_char {
let rust_cstr = unsafe { CStr::from_ptr(s) }; // &std::ffi::c_str::CStr
let str = rust_cstr.to_str().unwrap(); // &str
let string: String = str.chars().rev().collect(); // collections::string::String
let cstring = CString::new(string).unwrap(); // std::ffi::c_str::CString
cstring.into_ptr() // *const i8
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment