Skip to content

Fix code samples for closures #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
aa5631c
register snapshot
Jan 5, 2015
d7fcf2c
std: remove remaining boxed closures
Jan 1, 2015
65a6176
syntax: remove remaining boxed closures
Jan 1, 2015
064cf76
conv_did: convert to "unboxed" closure
Jan 3, 2015
e7926bc
DecodeInlinedItem: convert to "unboxed" closures
Jan 4, 2015
2a4b9b1
EncodeInlinedItem: convert to "unboxed" closures
Jan 4, 2015
01ea28a
rustc: remove remaining boxed closures
Jan 4, 2015
f62b291
typeck: remove remaining boxed closures
Jan 4, 2015
b41a8cf
trans: remove remaining boxed closures
Jan 4, 2015
0da0d0d
driver: remove unboxed closures
Jan 5, 2015
112ff9b
compiletest: remove boxed closures
Jan 5, 2015
f62c82b
coretest: remove/ignore tests
Jan 5, 2015
d8f6e6e
remove ty_closure
Jan 4, 2015
066e849
remove TyClosure
Jan 4, 2015
ee4b86e
remove AdjustAddEnv
Jan 4, 2015
9622783
remove mk_closure
Jan 4, 2015
05c2993
trans: remove Closure
Jan 5, 2015
59bce5b
syntax: make the closure type `f: |uint| -> bool` syntax obsolete
Jan 1, 2015
82c765f
typeck: there are only unboxed closures now
Jan 4, 2015
cdd37d6
syntax: remove dead code
Jan 4, 2015
6340706
rustc: remove dead code
Jan 4, 2015
689cb0e
trans: remove dead code
Jan 4, 2015
198a838
typeck: remove dead code
Jan 4, 2015
2ee0eb9
fix rpass tests
Jan 2, 2015
6a41f5c
fix cfail tests
Jan 3, 2015
be81944
fix run-make test
Jan 5, 2015
be3548f
fix pretty tests
Jan 5, 2015
ae5606e
fix debuginfo tests
Jan 5, 2015
b2e0af4
fix benchmarks
Jan 5, 2015
5a0a571
ignore boxed closure doctests in the guide/reference
Jan 5, 2015
0bfbc8e
address Niko's comments
Jan 5, 2015
56ca817
Fix code samples for closures
steveklabnik Jan 5, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 3 additions & 2 deletions src/compiletest/compiletest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,9 @@ pub fn is_test(config: &Config, testfile: &Path) -> bool {
return valid;
}

pub fn make_test(config: &Config, testfile: &Path, f: || -> test::TestFn)
-> test::TestDescAndFn {
pub fn make_test<F>(config: &Config, testfile: &Path, f: F) -> test::TestDescAndFn where
F: FnOnce() -> test::TestFn,
{
test::TestDescAndFn {
desc: test::TestDesc {
name: make_test_name(config, testfile),
Expand Down
4 changes: 3 additions & 1 deletion src/compiletest/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ pub fn is_test_ignored(config: &Config, testfile: &Path) -> bool {
!val
}

fn iter_header(testfile: &Path, it: |&str| -> bool) -> bool {
fn iter_header<F>(testfile: &Path, mut it: F) -> bool where
F: FnMut(&str) -> bool,
{
use std::io::{BufferedReader, File};

let mut rdr = BufferedReader::new(File::open(testfile).unwrap());
Expand Down
14 changes: 8 additions & 6 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1233,12 +1233,14 @@ enum TargetLocation {
ThisDirectory(Path),
}

fn make_compile_args(config: &Config,
props: &TestProps,
extras: Vec<String> ,
xform: |&Config, &Path| -> TargetLocation,
testfile: &Path)
-> ProcArgs {
fn make_compile_args<F>(config: &Config,
props: &TestProps,
extras: Vec<String> ,
xform: F,
testfile: &Path)
-> ProcArgs where
F: FnOnce(&Config, &Path) -> TargetLocation,
{
let xform_file = xform(config, testfile);
let target = if props.force_host {
config.host.as_slice()
Expand Down
4 changes: 2 additions & 2 deletions src/doc/guide-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ optimizer to consider the result used and ensures it cannot remove the
computation entirely. This could be done for the example above by adjusting the
`b.iter` call to

```rust
```{rust,ignore}
# struct X; impl X { fn iter<T>(&self, _: || -> T) {} } let b = X;
b.iter(|| {
// note lack of `;` (could also use an explicit `return`).
Expand All @@ -548,7 +548,7 @@ Or, the other option is to call the generic `test::black_box` function, which
is an opaque "black box" to the optimizer and so forces it to consider any
argument as used.

```rust
```{rust,ignore}
extern crate test;

# fn main() {
Expand Down
23 changes: 12 additions & 11 deletions src/doc/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -4231,7 +4231,7 @@ arguments, really powerful things are possible.

Let's make a closure:

```{rust}
```rust
let add_one = |x| { 1 + x };

println!("The sum of 5 plus 1 is {}.", add_one(5));
Expand All @@ -4243,7 +4243,7 @@ binding name and two parentheses, just like we would for a named function.

Let's compare syntax. The two are pretty close:

```{rust}
```rust
let add_one = |x: i32| -> i32 { 1 + x };
fn add_one (x: i32) -> i32 { 1 + x }
```
Expand All @@ -4256,7 +4256,7 @@ There's one big difference between a closure and named functions, and it's in
the name: a closure "closes over its environment." What does that mean? It means
this:

```{rust}
```rust
fn main() {
let x = 5;

Expand Down Expand Up @@ -4297,7 +4297,7 @@ now. We'll talk about them more in the "Threads" section of the guide.

Closures are most useful as an argument to another function. Here's an example:

```{rust}
```rust
fn twice(x: i32, f: |i32| -> i32) -> i32 {
f(x) + f(x)
}
Expand All @@ -4311,14 +4311,14 @@ fn main() {

Let's break the example down, starting with `main`:

```{rust}
```rust
let square = |x: i32| { x * x };
```

We've seen this before. We make a closure that takes an integer, and returns
its square.

```{rust}
```rust
# fn twice(x: i32, f: |i32| -> i32) -> i32 { f(x) + f(x) }
# let square = |x: i32| { x * x };
twice(5, square); // evaluates to 50
Expand All @@ -4332,8 +4332,9 @@ passing two variables: one is an i32, and one is a function."

Next, let's look at how `twice` is defined:

```{rust,ignore}
```rust
fn twice(x: i32, f: |i32| -> i32) -> i32 {
# 42 }
```

`twice` takes two arguments, `x` and `f`. That's why we called it with two
Expand All @@ -4342,7 +4343,7 @@ though, and that function takes an `i32` and returns an `i32`. Notice
how the `|i32| -> i32` syntax looks a lot like our definition of `square`
above, if we added the return type in:

```{rust}
```rust
let square = |x: i32| -> i32 { x * x };
// |i32| -> i32
```
Expand All @@ -4357,7 +4358,7 @@ Finally, `twice` returns an `i32` as well.

Okay, let's look at the body of `twice`:

```{rust}
```rust
fn twice(x: i32, f: |i32| -> i32) -> i32 {
f(x) + f(x)
}
Expand All @@ -4375,7 +4376,7 @@ this technique a lot.
If we didn't want to give `square` a name, we could just define it inline.
This example is the same as the previous one:

```{rust}
```rust
fn twice(x: i32, f: |i32| -> i32) -> i32 {
f(x) + f(x)
}
Expand All @@ -4388,7 +4389,7 @@ fn main() {
A named function's name can be used wherever you'd use a closure. Another
way of writing the previous example:

```{rust}
```rust
fn twice(x: i32, f: |i32| -> i32) -> i32 {
f(x) + f(x)
}
Expand Down
8 changes: 4 additions & 4 deletions src/doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1559,7 +1559,7 @@ Type parameters can be specified for a trait to make it generic. These appear
after the trait name, using the same syntax used in [generic
functions](#generic-functions).

```
``` ignore
trait Seq<T> {
fn len(&self) -> uint;
fn elt_at(&self, n: uint) -> T;
Expand Down Expand Up @@ -3217,7 +3217,7 @@ expression's captured environment.
In this example, we define a function `ten_times` that takes a higher-order
function argument, and call it with a lambda expression as an argument.

```
``` ignore
fn ten_times(f: |int|) {
let mut i = 0;
while i < 10 {
Expand Down Expand Up @@ -3821,7 +3821,7 @@ or `extern`), a sequence of input types and an output type.

An example of a `fn` type:

```
``` ignore
fn add(x: int, y: int) -> int {
return x + y;
}
Expand Down Expand Up @@ -3849,7 +3849,7 @@ The type of a closure mapping an input of type `A` to an output of type `B` is

An example of creating and calling a closure:

```rust
``` ignore
let captured_var = 10i;

let closure_no_args = || println!("captured_var={}", captured_var);
Expand Down
15 changes: 0 additions & 15 deletions src/libcollections/bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,6 @@ pub struct Bitv {
nbits: uint
}

// NOTE(stage0): remove impl after a snapshot
#[cfg(stage0)]
// FIXME(Gankro): NopeNopeNopeNopeNope (wait for IndexGet to be a thing)
impl Index<uint,bool> for Bitv {
#[inline]
fn index(&self, i: &uint) -> &bool {
if self.get(*i).expect("index out of bounds") {
&TRUE
} else {
&FALSE
}
}
}

#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
// FIXME(Gankro): NopeNopeNopeNopeNope (wait for IndexGet to be a thing)
impl Index<uint> for Bitv {
type Output = bool;
Expand Down
24 changes: 0 additions & 24 deletions src/libcollections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,18 +877,6 @@ impl<K: Show, V: Show> Show for BTreeMap<K, V> {
}
}

// NOTE(stage0): remove impl after a snapshot
#[cfg(stage0)]
#[stable]
impl<K: Ord, Sized? Q, V> Index<Q, V> for BTreeMap<K, V>
where Q: BorrowFrom<K> + Ord
{
fn index(&self, key: &Q) -> &V {
self.get(key).expect("no entry found for key")
}
}

#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
#[stable]
impl<K: Ord, Sized? Q, V> Index<Q> for BTreeMap<K, V>
where Q: BorrowFrom<K> + Ord
Expand All @@ -900,18 +888,6 @@ impl<K: Ord, Sized? Q, V> Index<Q> for BTreeMap<K, V>
}
}

// NOTE(stage0): remove impl after a snapshot
#[cfg(stage0)]
#[stable]
impl<K: Ord, Sized? Q, V> IndexMut<Q, V> for BTreeMap<K, V>
where Q: BorrowFrom<K> + Ord
{
fn index_mut(&mut self, key: &Q) -> &mut V {
self.get_mut(key).expect("no entry found for key")
}
}

#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
#[stable]
impl<K: Ord, Sized? Q, V> IndexMut<Q> for BTreeMap<K, V>
where Q: BorrowFrom<K> + Ord
Expand Down
22 changes: 0 additions & 22 deletions src/libcollections/ring_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1360,17 +1360,6 @@ impl<S: Writer, A: Hash<S>> Hash<S> for RingBuf<A> {
}
}

// NOTE(stage0): remove impl after a snapshot
#[cfg(stage0)]
#[stable]
impl<A> Index<uint, A> for RingBuf<A> {
#[inline]
fn index<'a>(&'a self, i: &uint) -> &'a A {
self.get(*i).expect("Out of bounds access")
}
}

#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
#[stable]
impl<A> Index<uint> for RingBuf<A> {
type Output = A;
Expand All @@ -1381,17 +1370,6 @@ impl<A> Index<uint> for RingBuf<A> {
}
}

// NOTE(stage0): remove impl after a snapshot
#[cfg(stage0)]
#[stable]
impl<A> IndexMut<uint, A> for RingBuf<A> {
#[inline]
fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut A {
self.get_mut(*i).expect("Out of bounds access")
}
}

#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
#[stable]
impl<A> IndexMut<uint> for RingBuf<A> {
type Output = A;
Expand Down
21 changes: 0 additions & 21 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1190,17 +1190,6 @@ impl<S: hash::Writer, T: Hash<S>> Hash<S> for Vec<T> {
}
}

// NOTE(stage0): remove impl after a snapshot
#[cfg(stage0)]
#[experimental = "waiting on Index stability"]
impl<T> Index<uint,T> for Vec<T> {
#[inline]
fn index<'a>(&'a self, index: &uint) -> &'a T {
&self.as_slice()[*index]
}
}

#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
#[experimental = "waiting on Index stability"]
impl<T> Index<uint> for Vec<T> {
type Output = T;
Expand All @@ -1211,16 +1200,6 @@ impl<T> Index<uint> for Vec<T> {
}
}

// NOTE(stage0): remove impl after a snapshot
#[cfg(stage0)]
impl<T> IndexMut<uint,T> for Vec<T> {
#[inline]
fn index_mut<'a>(&'a mut self, index: &uint) -> &'a mut T {
&mut self.as_mut_slice()[*index]
}
}

#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
impl<T> IndexMut<uint> for Vec<T> {
type Output = T;

Expand Down
22 changes: 0 additions & 22 deletions src/libcollections/vec_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,17 +517,6 @@ impl<V> Extend<(uint, V)> for VecMap<V> {
}
}

// NOTE(stage0): remove impl after a snapshot
#[cfg(stage0)]
#[stable]
impl<V> Index<uint, V> for VecMap<V> {
#[inline]
fn index<'a>(&'a self, i: &uint) -> &'a V {
self.get(i).expect("key not present")
}
}

#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
impl<V> Index<uint> for VecMap<V> {
type Output = V;

Expand All @@ -537,17 +526,6 @@ impl<V> Index<uint> for VecMap<V> {
}
}

// NOTE(stage0): remove impl after a snapshot
#[cfg(stage0)]
#[stable]
impl<V> IndexMut<uint, V> for VecMap<V> {
#[inline]
fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut V {
self.get_mut(i).expect("key not present")
}
}

#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
#[stable]
impl<V> IndexMut<uint> for VecMap<V> {
type Output = V;
Expand Down
Loading