Skip to content

Instantly share code, notes, and snippets.

fn main() {
let flip = |(x,y)| (y, x);
println(fmt!("%?", flip((-9i,0i))));
}
impl<K: Eq + Hash, V, T: Iterator<(K, V)>> FromIterator<(K, V), T> for HashMap<K, V> {
pub fn from_iterator(iter: &mut T) -> HashMap<K, V> {
let (lower, _) = iter.size_hint();
let mut map = HashMap::with_capacity(lower);
for iter.advance |(k, v)| {
map.insert(k, v);
}
map
pub trait Transformer<From,To, Err: ToStr> {
fn one_shot(&mut self, input: &[From]) -> Result<~[To],Err>;
fn stream(&mut self, input: &[From], blk: &fn(&[To]);
}
pub trait Codec<D,E, Err: ToStr> {
fn encoder(&self) -> Transformer<D,E,Err>;
fn decoder(&self) -> Transformer<E,D,Err>;
fn encode(&self, input: &[D]) -> Result<~[E],~Err>;
fn decode(&self, input: &[E]) -> Result<~[D],~Err>;
macro_rules! Ξ (
($name:expr) => (
($name).to_str()
)
)
println( Ξ!(1 * 6 + 6 - 8 * 5 + 5 * 7i));
start: BZ_OK
err: BZ_RUN_OK, buff_sz: 10000, stream.avail_out: 10000, buff_sz - stream,avail_out: 0
end: BZ_RUN_OK
start: BZ_RUN_OK
err: BZ_STREAM_END, buff_sz: 10000, stream.avail_out: 6887, buff_sz - stream,avail_out: 3113
end: BZ_STREAM_END
err: BZ_STREAM_END, buff_sz: 10000, stream.avail_out: 10000, buff_sz - stream,avail_out: 0
// curl/opt.rs
macro_rules! CINIT(
($name:ident, $typ:expr, $val:expr) => (
$name = $typ + $val,
)
)
static LONG: i32 = 0i32;
@MarkJr94
MarkJr94 / minimal_borrowck.rs
Created June 28, 2013 21:57
Minimal example of problem with borrowck, mutability, and trait objects.
// traitfail.rs
trait Slide {
fn new(x: int) -> Self;
fn x_mut<'r> (&'r mut self) -> &'r mut int;
fn x(&self) -> int;
}
struct Bead {
x: int
// mummy.rs
use pc::PC;
use common::{Character};
#[deriving(ToStr)]
pub struct Mummy {
hp: uint,
attack: uint,
x: int,
// listen.rs
use extra::net::ip;
use extra::net::tcp;
use std::task;
pub fn server_loop() {
use extra::uv::iotask::spawn_iotask;
let mut builder = task::task();
#[deriving(Eq,Rand)]
pub enum Card {
One = 1,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,