Skip to content

Instantly share code, notes, and snippets.

View mfpiccolo's full-sized avatar

Mike Piccolo mfpiccolo

View GitHub Profile
@mfpiccolo
mfpiccolo / a-rubyist-rusting-part-2-9.rs
Last active August 29, 2015 14:24
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
@mfpiccolo
mfpiccolo / a-rubyist-rusting-part-2-8.rs
Last active August 29, 2015 14:24
External reverse function convert string
extern crate libc;
use std::ffi::CStr;
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
// convert and return for ffi ruby module
}
@mfpiccolo
mfpiccolo / a-rubyist-rusting-part-2-7.rs
Last active November 11, 2015 08:05
Ruby extern rust function with string input and output
extern crate libc;
pub extern fn reverse(s: *const libc::c_char) -> *const libc::c_char {
// reverse string and return
}
fn reverse(s: String) -> String {
s.chars().rev().collect()
}
@mfpiccolo
mfpiccolo / a-rubyist-rusting-part-2-5.rs
Last active August 29, 2015 14:24
Reverse rust string test
#[test]
fn it_works() {
assert!(
reverse(“Don’t use palindrome”.to_string()) == “emordnilap esu t’noD”
);
}
fn reverse(s: String) -> String {
s.chars().rev()
}
fn reverse(s: String) -> String {
// reverse string here
}
#[test]
fn it_works() {
assert!(
reverse(“Don’t use palindrome”) == "emordnilap esu t'noD";
)
}
"abcde".chars.reduce{|s,c| c + s } # => "edcba"
'this is a string'.class # => String
"this is a string".class # => String
%{this is a string}.class # => String
string = <<END
this is a string
END
string.class = # => String
#[test]
fn it_works() {
assert!(true);
}