Skip to content

Instantly share code, notes, and snippets.

View steveklabnik's full-sized avatar
🦀
Rustacean

Steve Klabnik steveklabnik

🦀
Rustacean
View GitHub Profile
@steveklabnik
steveklabnik / store_champs.md
Last active February 7, 2016 14:04
NYC (and surrounding area) Meta Store Championships
extern crate gl;
extern crate glutin;
extern crate libc;
use std::io::prelude::*;
use std::io;
use std::fs::File;
fn main(){
let s = read_file("model.obj");
/// This is a test:
///
/// ```rust
/// let x = SomeUnknownStructure::new();
/// ```
fn foo() {}
@steveklabnik
steveklabnik / summary.md
Created September 29, 2015 14:39
my summary of "using Rust with Ruby: a deep dive with Yehuda Katz"

My summary of https://www.youtube.com/watch?v=IqrwPVtSHZI

TL;DR:

Rails has a library, ActiveSupport, which adds methods to Ruby core classes. One of those methods is String#blank?, which returns a boolean (sometimes I miss this convention in Rust, the ?) if the whole string is whitespace or not. It looks like this: https://github.com/rails/rails/blob/b3eac823006eb6a346f88793aabef28a6d4f928c/activesupport/lib/active_support/core_ext/object/blank.rb#L99-L117

It's pretty slow. So Discourse (which you may know from {users,internals}.rust-lang.org) uses the fast_blank gem, which provides this method via a C implementation instead. It looks like this: https://github.com/SamSaffron/fast_blank/blob/master/ext/fast_blank/fast_blank.c

For fun, Yehuda tried to re-write fast_blank in Rust. Which looks like this:

use std::collections::HashMap;
use std::fmt;
#[derive(PartialEq, Eq, Hash, Debug, Copy, Clone)]
pub enum Allergen {
Eggs = 1,
Strawberries = 8,
Peanuts = 2,
Shellfish = 4,
Chocolate = 32,
x = RubyVM::InstructionSequence.new(%q{
class Foo
def hello
"hello"
end
end
f = Foo.new
f.hello()
})
struct Foo;
impl Foo {
fn hello(&self) -> &str {
"hello"
}
}
fn main() {
let f = Foo;
@steveklabnik
steveklabnik / gist:18789c7d46b248dfcdb1
Created September 7, 2015 15:43
Issue with binding ownership and scopes in Rust
pub struct Command {
cmd: String,
}
impl Command {
pub fn new(cmd: &str) -> Command {
Command {
cmd: String::from(cmd),
}
}
@steveklabnik
steveklabnik / file.rs
Last active September 6, 2015 21:40 — forked from anonymous/file.rs
fn move_enemies(&mut self) {
self.enemies = self.enemies.into_iter().map(|enemy| {
let between = Range::new(0, 4);
let mut rng = rand::thread_rng();
let desired_move: (i8, i8) = match between.ind_sample(&mut rng) {
0 => (0, -1),
1 => (0, 1),
2 => (-1, 0),
3 => (1, 0),
_ => (0, 0), // This shouldn't happen.
@steveklabnik
steveklabnik / main.rs
Created August 18, 2015 20:04 — forked from shadoi/main.rs
#[macro_use]
extern crate nickel;
extern crate yaml_rust;
use nickel::{ Nickel, HttpRouter };
use yaml_rust::{ Yaml, YamlEmitter };
mod yaml_handler;
fn get_yaml() -> String {