Skip to content

Commit 97f870a

Browse files
author
Jorge Aparicio
committed
unignore and fix doctests in guide and reference
1 parent a55011e commit 97f870a

File tree

3 files changed

+42
-40
lines changed

3 files changed

+42
-40
lines changed

src/doc/guide-testing.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,9 @@ optimizer to consider the result used and ensures it cannot remove the
536536
computation entirely. This could be done for the example above by adjusting the
537537
`b.iter` call to
538538

539-
```{rust,ignore}
540-
# struct X; impl X { fn iter<T>(&self, _: || -> T) {} } let b = X;
539+
```rust
540+
# struct X;
541+
# impl X { fn iter<T, F>(&self, _: F) where F: FnMut() -> T {} } let b = X;
541542
b.iter(|| {
542543
// note lack of `;` (could also use an explicit `return`).
543544
range(0u, 1000).fold(0, |old, new| old ^ new)
@@ -548,11 +549,12 @@ Or, the other option is to call the generic `test::black_box` function, which
548549
is an opaque "black box" to the optimizer and so forces it to consider any
549550
argument as used.
550551

551-
```{rust,ignore}
552+
```rust
552553
extern crate test;
553554

554555
# fn main() {
555-
# struct X; impl X { fn iter<T>(&self, _: || -> T) {} } let b = X;
556+
# struct X;
557+
# impl X { fn iter<T, F>(&self, _: F) where F: FnMut() -> T {} } let b = X;
556558
b.iter(|| {
557559
test::black_box(range(0u, 1000).fold(0, |old, new| old ^ new));
558560
});

src/doc/guide.md

+26-26
Original file line numberDiff line numberDiff line change
@@ -4231,8 +4231,8 @@ arguments, really powerful things are possible.
42314231
42324232
Let's make a closure:
42334233
4234-
```{rust,ignore}
4235-
let add_one = |x| { 1 + x };
4234+
```{rust}
4235+
let add_one = |&: x| { 1 + x };
42364236
42374237
println!("The sum of 5 plus 1 is {}.", add_one(5));
42384238
```
@@ -4243,9 +4243,9 @@ binding name and two parentheses, just like we would for a named function.
42434243
42444244
Let's compare syntax. The two are pretty close:
42454245
4246-
```{rust,ignore}
4247-
let add_one = |x: i32| -> i32 { 1 + x };
4248-
fn add_one (x: i32) -> i32 { 1 + x }
4246+
```{rust}
4247+
let add_one = |&: x: i32| -> i32 { 1 + x };
4248+
fn add_one (x: i32) -> i32 { 1 + x }
42494249
```
42504250
42514251
As you may have noticed, closures infer their argument and return types, so you
@@ -4256,11 +4256,11 @@ There's one big difference between a closure and named functions, and it's in
42564256
the name: a closure "closes over its environment." What does that mean? It means
42574257
this:
42584258
4259-
```{rust,ignore}
4259+
```{rust}
42604260
fn main() {
4261-
let x = 5;
4261+
let x: i32 = 5;
42624262
4263-
let printer = || { println!("x is: {}", x); };
4263+
let printer = |&:| { println!("x is: {}", x); };
42644264
42654265
printer(); // prints "x is: 5"
42664266
}
@@ -4276,7 +4276,7 @@ defined. The closure borrows any variables it uses, so this will error:
42764276
fn main() {
42774277
let mut x = 5;
42784278
4279-
let printer = || { println!("x is: {}", x); };
4279+
let printer = |&:| { println!("x is: {}", x); };
42804280
42814281
x = 6; // error: cannot assign to `x` because it is borrowed
42824282
}
@@ -4297,30 +4297,30 @@ now. We'll talk about them more in the "Threads" section of the guide.
42974297
42984298
Closures are most useful as an argument to another function. Here's an example:
42994299
4300-
```{rust,ignore}
4301-
fn twice(x: i32, f: |i32| -> i32) -> i32 {
4300+
```{rust}
4301+
fn twice<F: Fn(i32) -> i32>(x: i32, f: F) -> i32 {
43024302
f(x) + f(x)
43034303
}
43044304
43054305
fn main() {
4306-
let square = |x: i32| { x * x };
4306+
let square = |&: x: i32| { x * x };
43074307
43084308
twice(5, square); // evaluates to 50
43094309
}
43104310
```
43114311
43124312
Let's break the example down, starting with `main`:
43134313
4314-
```{rust,ignore}
4315-
let square = |x: i32| { x * x };
4314+
```{rust}
4315+
let square = |&: x: i32| { x * x };
43164316
```
43174317
43184318
We've seen this before. We make a closure that takes an integer, and returns
43194319
its square.
43204320
4321-
```{rust,ignore}
4322-
# fn twice(x: i32, f: |i32| -> i32) -> i32 { f(x) + f(x) }
4323-
# let square = |x: i32| { x * x };
4321+
```{rust}
4322+
# fn twice<F: Fn(i32) -> i32>(x: i32, f: F) -> i32 { f(x) + f(x) }
4323+
# let square = |&: x: i32| { x * x };
43244324
twice(5, square); // evaluates to 50
43254325
```
43264326
@@ -4342,9 +4342,9 @@ though, and that function takes an `i32` and returns an `i32`. Notice
43424342
how the `|i32| -> i32` syntax looks a lot like our definition of `square`
43434343
above, if we added the return type in:
43444344
4345-
```{rust,ignore}
4346-
let square = |x: i32| -> i32 { x * x };
4347-
// |i32| -> i32
4345+
```{rust}
4346+
let square = |&: x: i32| -> i32 { x * x };
4347+
// |i32| -> i32
43484348
```
43494349
43504350
This function takes an `i32` and returns an `i32`.
@@ -4357,8 +4357,8 @@ Finally, `twice` returns an `i32` as well.
43574357
43584358
Okay, let's look at the body of `twice`:
43594359
4360-
```{rust,ignore}
4361-
fn twice(x: i32, f: |i32| -> i32) -> i32 {
4360+
```{rust}
4361+
fn twice<F: Fn(i32) -> i32>(x: i32, f: F) -> i32 {
43624362
f(x) + f(x)
43634363
}
43644364
```
@@ -4375,8 +4375,8 @@ this technique a lot.
43754375
If we didn't want to give `square` a name, we could just define it inline.
43764376
This example is the same as the previous one:
43774377
4378-
```{rust,ignore}
4379-
fn twice(x: i32, f: |i32| -> i32) -> i32 {
4378+
```{rust}
4379+
fn twice<F: Fn(i32) -> i32>(x: i32, f: F) -> i32 {
43804380
f(x) + f(x)
43814381
}
43824382
@@ -4388,8 +4388,8 @@ fn main() {
43884388
A named function's name can be used wherever you'd use a closure. Another
43894389
way of writing the previous example:
43904390
4391-
```{rust,ignore}
4392-
fn twice(x: i32, f: |i32| -> i32) -> i32 {
4391+
```{rust}
4392+
fn twice<F: Fn(i32) -> i32>(x: i32, f: F) -> i32 {
43934393
f(x) + f(x)
43944394
}
43954395

src/doc/reference.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -1559,11 +1559,11 @@ Type parameters can be specified for a trait to make it generic. These appear
15591559
after the trait name, using the same syntax used in [generic
15601560
functions](#generic-functions).
15611561

1562-
``` ignore
1562+
```
15631563
trait Seq<T> {
15641564
fn len(&self) -> uint;
15651565
fn elt_at(&self, n: uint) -> T;
1566-
fn iter(&self, |T|);
1566+
fn iter<F>(&self, F) where F: Fn(T);
15671567
}
15681568
```
15691569

@@ -3217,8 +3217,8 @@ expression's captured environment.
32173217
In this example, we define a function `ten_times` that takes a higher-order
32183218
function argument, and call it with a lambda expression as an argument.
32193219

3220-
``` ignore
3221-
fn ten_times(f: |int|) {
3220+
```
3221+
fn ten_times<F>(f: F) where F: Fn(int) {
32223222
let mut i = 0;
32233223
while i < 10 {
32243224
f(i);
@@ -3821,14 +3821,14 @@ or `extern`), a sequence of input types and an output type.
38213821

38223822
An example of a `fn` type:
38233823

3824-
``` ignore
3824+
```
38253825
fn add(x: int, y: int) -> int {
38263826
return x + y;
38273827
}
38283828
38293829
let mut x = add(5,7);
38303830
3831-
type Binop<'a> = |int,int|: 'a -> int;
3831+
type Binop = fn(int, int) -> int;
38323832
let bo: Binop = add;
38333833
x = bo(5,7);
38343834
```
@@ -3849,17 +3849,17 @@ The type of a closure mapping an input of type `A` to an output of type `B` is
38493849

38503850
An example of creating and calling a closure:
38513851

3852-
``` ignore
3852+
```rust
38533853
let captured_var = 10i;
38543854

3855-
let closure_no_args = || println!("captured_var={}", captured_var);
3855+
let closure_no_args = |&:| println!("captured_var={}", captured_var);
38563856

3857-
let closure_args = |arg: int| -> int {
3857+
let closure_args = |&: arg: int| -> int {
38583858
println!("captured_var={}, arg={}", captured_var, arg);
38593859
arg // Note lack of semicolon after 'arg'
38603860
};
38613861

3862-
fn call_closure(c1: ||, c2: |int| -> int) {
3862+
fn call_closure<F: Fn(), G: Fn(int) -> int>(c1: F, c2: G) {
38633863
c1();
38643864
c2(2);
38653865
}

0 commit comments

Comments
 (0)