Skip to content

Instantly share code, notes, and snippets.

fn tail<A,IA:iterable<A>>(
iter: IA, op: fn(A)) {
let first = true;
iter.iter {|e|
if !first {
op(e);
}
first = false;
}
}
fn foo() -> @memory_pool {
// let the region for this block be "r_foo"
let p: @memory_pool = pool();
let x: &r_foo.T = new(p) T;
ret p; // x cannot be returned because r_foo is not in scope
}
fn bar() {
let p = foo(); // memory for p is not yet freed...
// ...but I can't access the prior contents.
#[non_copyable]
enum one_shot<A,T> = fn~(A) -> T;
fn execute(-f: one_shot<A>, a: A) -> T { (*f)(a) }
fn mk_one_shot_adder(-v1: ~int) -> one_shot<int,int> {
let v1 = ~some(move v1); // this "move" is not yet legal syntax
ret one_shot(fn~[move v1](v2: int) -> int {
let x = none;
x <-> v1;
@nikomatsakis
nikomatsakis / gist:2047617
Created March 15, 2012 23:20
should this compile?
iface i { fn foo() -> self<>; }
fn foo<T:i>(v: T) -> T {
ret v.foo();
}
iface i { fn foo() -> self<>; }
fn foo<T:i>(v: T) -> T {
ret v.foo();
}
impl of i for uint {
fn foo() -> uint { ret self; }
}
fn main() {
let x: i = 3u as i;
let y = foo(x); // y has type i, but is in fact a uint
enum t {
ty_nil,
}
fn mk_nil<C:ty_ops>(cx: C) -> t {
cx.mk()
}
iface ty_ops {
fn mk() -> t;
fn concat<T: copy>(v: [const [const T]]) -> [T] {
let mut r: [T] = [];
for inner: [T] in v { r += inner; }
ret r;
}
fn with<T,R>(v: T, f: f(T) -> R) -> R {
f(v)
}
fn some_func(a: uint, b: uint) -> uint {
with(
fn&(x: uint) -> uint { a + b + x }
) {|helper|
helper(22)
impl y for option<int> {
fn hi() { io::print("int"); }
}
impl x for option<uint> {
fn hi() { io::print("uint"); }
}
fn to_uint(x: option<uint>) {}
fn normalize_ty(cx: ctxt, t0: t) -> t {
fold_sty_to_ty(get(t0).struct) { |t|
normalize_ty(cx, t)
}
}