auto merge of #15929 : pcwalton/rust/by-ref-closures, r=alexcrichton

by-reference upvars.

This partially implements RFC 38. A snapshot will be needed to turn this
on, because stage0 cannot yet parse the keyword.

Part of #12831.

r? @alexcrichton
This commit is contained in:
bors 2014-08-14 03:46:22 +00:00
commit 9d45d63d0d
29 changed files with 254 additions and 72 deletions

View file

@ -0,0 +1,23 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn each<T>(x: &[T], f: |&T|) {
for val in x.iter() {
f(val)
}
}
fn main() {
let mut sum = 0u;
let elems = [ 1u, 2, 3, 4, 5 ];
each(elems, ref |val| sum += *val);
assert_eq!(sum, 15);
}

View file

@ -0,0 +1,29 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// ignore-test
//
// This is ignored because it depends on #16122.
#![feature(overloaded_calls, unboxed_closures)]
fn each<'a,T,F:|&mut: &'a T|>(x: &'a [T], mut f: F) {
for val in x.iter() {
f(val)
}
}
fn main() {
let mut sum = 0u;
let elems = [ 1u, 2, 3, 4, 5 ];
each(elems, ref |&mut: val: &uint| sum += *val);
assert_eq!(sum, 15);
}