Skip to content

Instantly share code, notes, and snippets.

@marijnh
Created December 16, 2011 21:25
Show Gist options
  • Save marijnh/1488059 to your computer and use it in GitHub Desktop.
Save marijnh/1488059 to your computer and use it in GitHub Desktop.
Rust implementation demo
impl util for uint {
fn str() -> str { uint::str(self) }
fn times(f: block(uint)) {
let c = 0u;
while c < self { f(c); c += 1u; }
}
}
impl util<T> for [T] {
fn len() -> uint { vec::len(self) }
fn iter(f: block(T)) { for x in self { f(x); } }
fn map<U>(f: block(T) -> U) -> [U] {
let r = [];
for elt in self { r += [f(elt)]; }
r
}
}
fn main() {
log_err [1].len().str();
log_err [3, 4].map({|a| a + 4});
10u.times({|n| log_err n;});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment