Skip to content

Instantly share code, notes, and snippets.

Avatar

bstrie bstrie

View GitHub Profile
View Cargo.toml
[package]
name="bob"
version="0.0.1"
authors=["insaneinside"]
[lib]
name="bob"
path="lib.rs"
View Comparison Problem
#[deriving(PartialEq)]
enum Ordering {
Less,
Equal,
Greater,
}
fn cmp(a: int, b: int) -> Ordering {
if a < b { Less }
else if a > b { Greater }
View bug.rs
pub enum Thing {
A(~Foo)
}
pub trait Foo {}
pub struct Struct;
impl Foo for Struct {}
View mitsuhiko
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));
}
}
View bstrie
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));
View gist:4947730
/* 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 {
View gist:4492901
/*
* This is a first attempt at dynamic Rust channels
*
* Paul Harvey
* 8 jan 2013
*/
//This should be replaced by generics
enum Msg_Type{
integer,
View gist:4476802
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";
)
...
}
View Rust unique pointer borrowing
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 Nov 21, 2012
Rust trait return
View gist:4128032
trait A {}
impl ~[int] : A {}
fn foo()-> A {
~[0]
}