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"
[package]
name="bob"
version="0.0.1"
[lib]
name="bob"
path="lib.rs"
Tremendously hacky measurement of the arity of function signatures in the Rust repository:
0 12919 48.10%
1 7703 28.68%
2 3388 12.61%
3 1544 5.74%
4 705 2.62%
5 345 1.28%
6 144 0.53%
7 58 0.21%
#[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 / area.rs
Last active August 29, 2015 14:08
trait HasArea {
fn area(&self) -> f64;
}
trait RectangularArea: HasArea {
fn area(&self) -> f64 {
self.length() * self.width()
}
fn length(&self) -> f64;
fn width(&self) -> f64;
<std macros>:2:59: 2:65 error: mismatched types: expected `()`, found `core::result::Result<<generic #9>,()>` (expected (), found enum core::result::Result)
<std macros>:2 ($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) })
^~~~~~
<std macros>:1:1: 3:2 note: in expansion of try!
try.rs:2:13: 2:25 note: expansion site
trait Foo {
fn new() -> Self;
}
struct Bar(int, int);
impl Foo for Bar {
fn new() -> Bar {
Bar(1, 2)
}
@bstrie
bstrie / iota.rs
Last active August 29, 2015 14:01
#![feature(macro_rules)]
macro_rules! iota(
($($thing:expr),+) => (iota_expr!(1, $($thing),*));
($($thing:pat),+) => (iota_pat!(1, $($thing),*));
() => (());
)
macro_rules! iota_expr(
($id:expr, $head:expr, $($tail:expr),*) => ({
#![feature(macro_rules)]
macro_rules! vec_new(
($($e:expr),*) => ({
// leading _ to allow empty construction without a warning.
let mut _temp = ::std::vec::Vec::with_capacity(arity!($($e),*));
$(_temp.push($e);)*
_temp
});
($($e:expr),+,) => (vec_new!($($e),+))
#![feature(macro_rules)]
macro_rules! foo {
($id:expr, $head:expr, $($tail:expr),+) => ({
foo!($id, $head);
foo!($id+1, $($tail),+);
});
($id:expr, $var:expr) => ({
println!(" {} = {},", $var, $id);
});