Skip to content

Instantly share code, notes, and snippets.

View bheylin's full-sized avatar

Brian Heylin bheylin

View GitHub Profile
#include <script/script_environment.hpp>
#include <lua.hpp>
#include <stdlib.h>
#include <iostream>
using namespace input;
using namespace render;
using namespace std;
@bheylin
bheylin / &mut copy
Created December 26, 2013 00:02
&mut copy derp
struct Context {
index: uint
}
fn main () {
let mut ctx = ~Context::new();
println!("test A");
ctx.snazzy_fn(|ctx_| {
println!("start action index:{}", ctx_.index);
ctx_.index += 1;
@bheylin
bheylin / move_test.rs
Created December 25, 2013 20:49
move_test
fn main() {
let p = Parent::new(~"pocket full of rain");
println!("Parent txt: {}", p.txt);
}
struct Parent<'a> {
txt: ~str,
data: ~ChildResult<'a>
}
@bheylin
bheylin / EDN
Created December 21, 2013 23:25
dead code warning in rust lib
#[pkgid="github.com/brianheylin/edn#0.1"];
#[link(name="edn")];
#[desc = "An EDN parser"];
#[license = "BSD"];
#[crate_type = "lib"];
#[deriving(ToStr, Eq, Default)]
pub struct Document {
txt: ~str,
ast: EDN
@bheylin
bheylin / Stack closures loop usage
Created October 7, 2013 17:09
Stack closures loop usage
fn main() {
let default_int = |_: &str| { 0 };
let keys = ["keyA", "keyB"];
// what is the cost of creating a closure in a loop?
for key in keys.iter() {
let i = get_or_else(*key, |k: &str| { default_int(k) });
println!("i: {}", i);
}
extern mod extra;
use extra::treemap::TreeMap;
type Hash = TreeMap<~str, ~str>;
fn insert_hash(hash: &mut Hash) {
let key = &~"key";
let find = || {
// main.rs:15:12: 15:16 error: cannot borrow `*hash` as mutable more than once at a time
// main.rs:15 hash.insert(~"key", ~"value");
// ^~~~
// main.rs:11:10: 11:14 note: second borrow of `*hash` as mutable occurs here
// main.rs:11 match hash.find_mut(key) {
// ^~~~
extern mod extra;
use extra::treemap::TreeMap;
static LBRACK: &'static char = &'[';
static RBRACK: &'static char = &']';
fn main() {
let ch = '[';
let msg = match ch {
RBRACK => "right",
LBRACK => "left"
@bheylin
bheylin / Tutorial: Owned boxes
Created September 11, 2013 20:12
The 0.8 tutorial states that the following code should throw a compile time error. There is no message supplied at compile time, but the run fails with code -10
struct Foo {
child: Option<Foo>
}
fn main() {
let foo = Foo { child: None };
info!("foo: %?", foo);
}
// >>>
@bheylin
bheylin / gist:6054343
Created July 22, 2013 14:41
Rust crash
rust|master ⇒ rust
The rust tool is a convenience for managing rust source code.
It acts as a shortcut for programs of the rust tool chain.
Usage: rust <command> [arguments]
The commands are:
build compile rust source files
run build an executable, and run it