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-1-1.rs
Last active August 29, 2015 14:24
Initial sum function
fn sum(x: i32, y: i32) -> i32 {
x + y
}
#[test]
fn it_works() {
assert!(sum(1, 2) == 3);
}
[package]
name = "scrape"
version = "0.1.0"
authors = ["Mike Piccolo <mfpiccolo@gmail.com>"]
[lib]
name = "embed"
crate-type = ["dylib"]
@mfpiccolo
mfpiccolo / a-rubyist-rusting-part-1-3.rs
Created July 5, 2015 19:25
External Rust Function
#[no_mangle]
pub extern fn sum(x: i32, y: i32) -> i32 {
x + y
}
source ‘https://rubygems.org'
gem ‘ffi’
require ‘ffi’
module Scrape
extend FFI::Library
ffi_lib ‘./debug/libscrape.dylib’
attach_function :sum, [:int32, :int32], :int32
end
puts Scrape.sum(2, 3) == 5 ? “Gone Dun It!” : “Uhhhh????”
#[test]
fn it_works() {
assert!(true);
}
'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
"abcde".chars.reduce{|s,c| c + s } # => "edcba"
fn reverse(s: String) -> String {
// reverse string here
}
#[test]
fn it_works() {
assert!(
reverse(“Don’t use palindrome”) == "emordnilap esu t'noD";
)
}
fn reverse(s: String) -> String {
s.chars().rev()
}