Last active
August 29, 2015 14:24
-
-
Save mfpiccolo/6e2b92af1ef50c12ff22 to your computer and use it in GitHub Desktop.
Final ruby reverse string
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
#![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