Skip to content

Instantly share code, notes, and snippets.

@huonw
huonw / util.rs
Last active December 17, 2015 20:38 — forked from lilyball/util.rs
// ...
pub trait VecUtil<Elem> {
pub fn grouped<'r>(&'r self, n: uint) -> ~[&'r [Elem]];
}
impl<'self, Elem> VecUtil<Elem> for &'self [Elem] {
fn grouped<'r>(&'r self, n: uint) -> ~[&'r [Elem]] {
if self.is_empty() { return ~[]; }
let mut res = vec::with_capacity((self.len()-1) / n + 1);
@huonw
huonw / echo.rs
Last active December 17, 2015 22:38 — forked from brendanzab/echo.rs
fn main() {
let args = os::args();
match args.tail() {
[~"-n",..strs] => print(str::connect(strs, " ")),
strs => println(str::connect(strs, " ")),
}
}
@huonw
huonw / main.rs
Last active December 18, 2015 01:39 — forked from anonymous/main.rs
pub mod other;
fn main() {
other::hello();
}
@huonw
huonw / gist:6034302
Last active December 19, 2015 23:19 — forked from bblum/gist:3885017
trait Nat { }
struct Zero;
struct Suc<N>(N);
impl Nat for Zero { }
impl<N: Nat> Nat for Suc<N> { }
@huonw
huonw / oops.rs
Last active December 20, 2015 06:39 — forked from kemurphy/oops.rs
let mut f = None;
match *e {
expr_call(f, ref args, _) => match f.node {
expr_path(path) => match dm.find(&f.id) {
Some(&v) => match v {
ast::def_variant(*) |ast::def_struct(*) => {
f = Some(expr_struct(
fld.fold_path(&path),
args.map(|x| fold_unnamed_field(*x)),
None
@huonw
huonw / t1.rs
Last active December 20, 2015 15:39 — forked from dovreshef/t1.rs
use t2::t3::fun;
mod t2;
fn main() {
t2::fun();
fun(); // calling t3::fun
}
@huonw
huonw / gist:6239071
Last active December 21, 2015 02:59 — forked from alexcrichton/gist:6239056
use std::rt::io;
struct HttpResponse<'self> {
out: &'self mut io::Writer,
}
fn write_http_header(_: &mut &mut io::Writer) {}
// fn write_http_header<W: io::Writer>(_: &mut W) {}
impl<'self> HttpResponse<'self> {
trait A {}
fn foo<T: A>(_: &T) {}
struct B;
impl A for B {}
impl<'self> A for &'self A {}
fn main() {
let a = B;
@huonw
huonw / gist:7351219
Created November 7, 2013 08:41 — forked from cnd/gist:7351205
fn ifV<'a, T>(rc : &str, star: &'a T) -> Option<&'a T> {
if (Path::new( rc )).exists() {
Some(star)
} else {
None
}
}
@huonw
huonw / gist:8366072
Created January 11, 2014 02:06 — forked from brson/gist:8366039
macro_rules! report_diag (
($f: tt, $name: tt, $msg: tt, $(arg: tt)*) => { {
reg_diag_msg!($name, $msg);
let msg = format!($msg, $($arg)*);
let msg = format!("{}: {}", stringify!($name), msg);
$f(msg);
} };
($f: tt, $name: tt, $msg: tt) => { {
reg_diag_msg!($name, $msg);
let msg = format!("{}: {}", stringify!($name), $msg);