Skip to content

Instantly share code, notes, and snippets.

View bstrie's full-sized avatar

bstrie bstrie

View GitHub Profile
[package]
name="bob"
version="0.0.1"
authors=["insaneinside"]
[lib]
name="bob"
path="lib.rs"
#[deriving(PartialEq)]
enum Ordering {
Less,
Equal,
Greater,
}
fn cmp(a: int, b: int) -> Ordering {
if a < b { Less }
else if a > b { Greater }
@bstrie
bstrie / bug.rs
Created May 8, 2013 00:44 — forked from wilkie/bug.rs
pub enum Thing {
A(~Foo)
}
pub trait Foo {}
pub struct Struct;
impl Foo for Struct {}
@bstrie
bstrie / mitsuhiko
Created February 26, 2013 20:17 — forked from mitsuhiko/bstrie
fn main() {
let numbers = [1, 2, 3];
let keys = ["foo", "bar", "baz"];
for (index, (&number, &key)): iter::enumerate(iter::zip(numbers, keys)) {
io::println(fmt!("%u: %s => %d", index, key, number));
}
}
@bstrie
bstrie / bstrie
Last active December 14, 2015 06:19 — forked from mitsuhiko/gist:5041421
fn main() {
let numbers = [1, 2, 3];
let keys = ["foo", "bar", "baz"];
for iter::zip(numbers, keys) |&number, &key| {
io::println(fmt!("%s => %d", key, number));
}
for iter::enumerate(keys) |&index, &key| {
io::println(fmt!("%u => %s", index, key));
/* a macro that defines a context variable that is stored in thread local
storage. It's implemented as a module that exports a `get`, `set` and
`set_to` function. `set_to` acts as a stack. */
macro_rules! _context_var_impl (($name:ident : $t:ty, $primer:expr, $is_primed:expr) => (
mod $name {
/* internal tls key helper */
fn key(_x: @$t) {}
/* primes a context variable from a callback */
fn prime(f: fn() -> @$t) -> @$t {
@bstrie
bstrie / gist:4492901
Last active December 10, 2015 21:08 — forked from anonymous/gist:4492878
/*
* This is a first attempt at dynamic Rust channels
*
* Paul Harvey
* 8 jan 2013
*/
//This should be replaced by generics
enum Msg_Type{
integer,
@bstrie
bstrie / gist:4476802
Last active December 10, 2015 18:48 — forked from Kimundi/gist:4476783
fn foo(a: Option<int>, b: Option<int>, c: Option<int>) {
arg_fail!( in "foo"
if matches!(a: None) : "a missing";
if matches!(b: Some(_)) : "Don't need b";
if matches!(b: None) && matches!(c: None) : "Need neither b nor c";
)
...
}
struct A {
v :~[int],
}
impl A {
fn foo(&mut self) {
//error: illegal borrow unless pure: unique value in aliasable, mutable location
let pa = &mut self.v[1];
//note: impure due to assigning to dereference of mutable & pointer
*pa = 5;
@bstrie
bstrie / gist:4128032
Created November 21, 2012 21:44
Rust trait return
trait A {}
impl ~[int] : A {}
fn foo()-> A {
~[0]
}