Skip to content

Commit 078bd49

Browse files
committed
Evaluate # fn in docs
I searched for times when we were hiding functions with # in the documentation, and fixed them to not use it unless neccesary. I also made random improvements whenever I changed something. For example, I changed Example to Examples, for consistency. Fixes #13423
1 parent a03701d commit 078bd49

File tree

14 files changed

+95
-102
lines changed

14 files changed

+95
-102
lines changed

src/doc/trpl/threads.md

+47-35
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@ closure is limited to capturing `Send`-able data from its environment
5151
ensures that `spawn` can safely move the entire closure and all its
5252
associated state into an entirely different thread for execution.
5353

54-
```{rust,ignore}
55-
# use std::thread::spawn;
56-
# fn generate_thread_number() -> int { 0 }
54+
```rust
55+
use std::thread::Thread;
56+
57+
fn generate_thread_number() -> i32 { 4 } // a very simple generation
58+
5759
// Generate some state locally
5860
let child_thread_number = generate_thread_number();
5961

60-
spawn(move || {
62+
Thread::spawn(move || {
6163
// Capture it in the remote thread. The `move` keyword indicates
6264
// that this closure should move `child_thread_number` into its
6365
// environment, rather than capturing a reference into the
@@ -77,40 +79,44 @@ The simplest way to create a channel is to use the `channel` function to create
7779
of a channel, and a *receiver* is the receiving endpoint. Consider the following
7880
example of calculating two results concurrently:
7981

80-
```{rust,ignore}
81-
# use std::thread::spawn;
82+
```rust
83+
use std::thread::Thread;
84+
use std::sync::mpsc;
8285

83-
let (tx, rx): (Sender<int>, Receiver<int>) = channel();
86+
let (tx, rx): (mpsc::Sender<u32>, mpsc::Receiver<u32>) = mpsc::channel();
8487

85-
spawn(move || {
88+
Thread::spawn(move || {
8689
let result = some_expensive_computation();
8790
tx.send(result);
8891
});
8992

9093
some_other_expensive_computation();
9194
let result = rx.recv();
92-
# fn some_expensive_computation() -> int { 42 }
93-
# fn some_other_expensive_computation() {}
95+
96+
fn some_expensive_computation() -> u32 { 42 } // very expensive ;)
97+
fn some_other_expensive_computation() {} // even more so
9498
```
9599

96100
Let's examine this example in detail. First, the `let` statement creates a
97101
stream for sending and receiving integers (the left-hand side of the `let`,
98102
`(tx, rx)`, is an example of a destructuring let: the pattern separates a tuple
99103
into its component parts).
100104

101-
```{rust,ignore}
102-
let (tx, rx): (Sender<int>, Receiver<int>) = channel();
105+
```rust
106+
# use std::sync::mpsc;
107+
let (tx, rx): (mpsc::Sender<u32>, mpsc::Receiver<u32>) = mpsc::channel();
103108
```
104109

105110
The child thread will use the sender to send data to the parent thread, which will
106111
wait to receive the data on the receiver. The next statement spawns the child
107112
thread.
108113

109-
```{rust,ignore}
110-
# use std::thread::spawn;
111-
# fn some_expensive_computation() -> int { 42 }
112-
# let (tx, rx) = channel();
113-
spawn(move || {
114+
```rust
115+
# use std::thread::Thread;
116+
# use std::sync::mpsc;
117+
# fn some_expensive_computation() -> u32 { 42 }
118+
# let (tx, rx) = mpsc::channel();
119+
Thread::spawn(move || {
114120
let result = some_expensive_computation();
115121
tx.send(result);
116122
});
@@ -125,9 +131,10 @@ computation, then sends the result over the captured channel.
125131
Finally, the parent continues with some other expensive computation, then waits
126132
for the child's result to arrive on the receiver:
127133

128-
```{rust,ignore}
134+
```rust
135+
# use std::sync::mpsc;
129136
# fn some_other_expensive_computation() {}
130-
# let (tx, rx) = channel::<int>();
137+
# let (tx, rx) = mpsc::channel::<u32>();
131138
# tx.send(0);
132139
some_other_expensive_computation();
133140
let result = rx.recv();
@@ -140,8 +147,9 @@ single `Receiver` value. What if our example needed to compute multiple
140147
results across a number of threads? The following program is ill-typed:
141148

142149
```{rust,ignore}
143-
# fn some_expensive_computation() -> int { 42 }
144-
let (tx, rx) = channel();
150+
# use std::sync::mpsc;
151+
# fn some_expensive_computation() -> u32 { 42 }
152+
let (tx, rx) = mpsc::channel();
145153
146154
spawn(move || {
147155
tx.send(some_expensive_computation());
@@ -156,19 +164,22 @@ spawn(move || {
156164

157165
Instead we can clone the `tx`, which allows for multiple senders.
158166

159-
```{rust,ignore}
160-
let (tx, rx) = channel();
167+
```rust
168+
use std::thread::Thread;
169+
use std::sync::mpsc;
170+
171+
let (tx, rx) = mpsc::channel();
161172

162-
for init_val in range(0u, 3) {
173+
for init_val in 0 .. 3 {
163174
// Create a new channel handle to distribute to the child thread
164175
let child_tx = tx.clone();
165-
spawn(move || {
176+
Thread::spawn(move || {
166177
child_tx.send(some_expensive_computation(init_val));
167178
});
168179
}
169180

170-
let result = rx.recv() + rx.recv() + rx.recv();
171-
# fn some_expensive_computation(_i: uint) -> int { 42 }
181+
let result = rx.recv().unwrap() + rx.recv().unwrap() + rx.recv().unwrap();
182+
# fn some_expensive_computation(_i: u32) -> u32 { 42 }
172183
```
173184

174185
Cloning a `Sender` produces a new handle to the same channel, allowing multiple
@@ -181,21 +192,22 @@ Note that the above cloning example is somewhat contrived since you could also
181192
simply use three `Sender` pairs, but it serves to illustrate the point. For
182193
reference, written with multiple streams, it might look like the example below.
183194

184-
```{rust,ignore}
185-
# use std::thread::spawn;
195+
```rust
196+
use std::thread::Thread;
197+
use std::sync::mpsc;
186198

187199
// Create a vector of ports, one for each child thread
188-
let rxs = Vec::from_fn(3, |init_val| {
189-
let (tx, rx) = channel();
190-
spawn(move || {
200+
let rxs = (0 .. 3).map(|&:init_val| {
201+
let (tx, rx) = mpsc::channel();
202+
Thread::spawn(move || {
191203
tx.send(some_expensive_computation(init_val));
192204
});
193205
rx
194-
});
206+
}).collect::<Vec<_>>();
195207

196208
// Wait on each port, accumulating the results
197-
let result = rxs.iter().fold(0, |accum, rx| accum + rx.recv() );
198-
# fn some_expensive_computation(_i: uint) -> int { 42 }
209+
let result = rxs.iter().fold(0, |&:accum, rx| accum + rx.recv().unwrap() );
210+
# fn some_expensive_computation(_i: u32) -> u32 { 42 }
199211
```
200212

201213
## Backgrounding computations: Futures

src/libcore/cell.rs

-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@
115115
//! }
116116
//! # fn calc_span_tree(&self) -> Vec<(uint, uint)> { vec![] }
117117
//! }
118-
//! # fn main() { }
119118
//! ```
120119
//!
121120
//! ## Mutating implementations of `clone`

src/libcore/finally.rs

-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@
2323
//!
2424
//! use std::finally::Finally;
2525
//!
26-
//! # fn main() {
2726
//! (|&mut:| {
2827
//! // ...
2928
//! }).finally(|| {
3029
//! // this code is always run
3130
//! })
32-
//! # }
3331
//! ```
3432
3533
#![deprecated = "It is unclear if this module is more robust than implementing \

src/libcore/macros.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@ macro_rules! panic {
3939
/// // the panic message for these assertions is the stringified value of the
4040
/// // expression given.
4141
/// assert!(true);
42-
/// # fn some_computation() -> bool { true }
42+
///
43+
/// fn some_computation() -> bool { true } // a very simple function
44+
///
4345
/// assert!(some_computation());
4446
///
4547
/// // assert with a custom message
46-
/// # let x = true;
48+
/// let x = true;
4749
/// assert!(x, "x wasn't true!");
48-
/// # let a = 3i; let b = 27i;
50+
///
51+
/// let a = 3i; let b = 27i;
4952
/// assert!(a + b == 30, "a = {}, b = {}", a, b);
5053
/// ```
5154
#[macro_export]
@@ -108,13 +111,15 @@ macro_rules! assert_eq {
108111
/// // the panic message for these assertions is the stringified value of the
109112
/// // expression given.
110113
/// debug_assert!(true);
111-
/// # fn some_expensive_computation() -> bool { true }
114+
///
115+
/// fn some_expensive_computation() -> bool { true } // a very simple function
112116
/// debug_assert!(some_expensive_computation());
113117
///
114118
/// // assert with a custom message
115-
/// # let x = true;
119+
/// let x = true;
116120
/// debug_assert!(x, "x wasn't true!");
117-
/// # let a = 3i; let b = 27i;
121+
///
122+
/// let a = 3; let b = 27;
118123
/// debug_assert!(a + b == 30, "a = {}, b = {}", a, b);
119124
/// ```
120125
#[macro_export]

src/libcore/marker.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,7 @@ impl<T: ?Sized> Clone for ContravariantType<T> {
210210
/// "interior" mutability:
211211
///
212212
/// ```
213-
/// pub struct Cell<T> { value: T }
214-
/// # fn main() {}
213+
/// struct Cell<T> { value: T }
215214
/// ```
216215
///
217216
/// The type system would infer that `value` is only read here and

src/libcore/prelude.rs

-4
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@
2121
//! # Example
2222
//!
2323
//! ```ignore
24-
//! # fn main() {
25-
//! #![feature(globs)]
26-
//!
2724
//! use core::prelude::*;
28-
//! # }
2925
//! ```
3026
3127
// Reexported core operators

src/libcore/result.rs

-2
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,9 @@
217217
//! makes it clear:
218218
//!
219219
//! ```
220-
//! # #![feature(macro_rules)]
221220
//! macro_rules! try {
222221
//! ($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) })
223222
//! }
224-
//! # fn main() { }
225223
//! ```
226224
//!
227225
//! `try!` is imported by the prelude, and is available everywhere.

src/libstd/fmt.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@
2626
//!
2727
//! Some examples of the `format!` extension are:
2828
//!
29-
//! ```rust
30-
//! # fn main() {
29+
//! ```
3130
//! format!("Hello"); // => "Hello"
3231
//! format!("Hello, {}!", "world"); // => "Hello, world!"
3332
//! format!("The number is {}", 1i); // => "The number is 1"
3433
//! format!("{:?}", (3i, 4i)); // => "(3i, 4i)"
3534
//! format!("{value}", value=4i); // => "4"
3635
//! format!("{} {}", 1i, 2u); // => "1 2"
37-
//! # }
3836
//! ```
3937
//!
4038
//! From these, you can see that the first argument is a format string. It is
@@ -83,12 +81,10 @@
8381
//!
8482
//! For example, the following `format!` expressions all use named argument:
8583
//!
86-
//! ```rust
87-
//! # fn main() {
84+
//! ```
8885
//! format!("{argument}", argument = "test"); // => "test"
8986
//! format!("{name} {}", 1i, name = 2i); // => "2 1"
9087
//! format!("{a} {c} {b}", a="a", b='b', c=3i); // => "a 3 b"
91-
//! # }
9288
//! ```
9389
//!
9490
//! It is illegal to put positional parameters (those without names) after
@@ -288,8 +284,6 @@
288284
//! use std::fmt;
289285
//! use std::io;
290286
//!
291-
//! # #[allow(unused_must_use)]
292-
//! # fn main() {
293287
//! fmt::format(format_args!("this returns {}", "String"));
294288
//!
295289
//! let some_writer: &mut io::Writer = &mut io::stdout();
@@ -299,7 +293,6 @@
299293
//! write!(&mut io::stdout(), "{}", args);
300294
//! }
301295
//! my_fmt_fn(format_args!("or a {} too", "function"));
302-
//! # }
303296
//! ```
304297
//!
305298
//! The result of the `format_args!` macro is a value of type `fmt::Arguments`.

src/libstd/io/mod.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -934,16 +934,15 @@ unsafe fn slice_vec_capacity<'a, T>(v: &'a mut Vec<T>, start: uint, end: uint) -
934934
/// A `RefReader` is a struct implementing `Reader` which contains a reference
935935
/// to another reader. This is often useful when composing streams.
936936
///
937-
/// # Example
937+
/// # Examples
938938
///
939939
/// ```
940-
/// # fn main() {}
941-
/// # fn process_input<R: Reader>(r: R) {}
942-
/// # fn foo() {
943940
/// use std::io;
944941
/// use std::io::ByRefReader;
945942
/// use std::io::util::LimitReader;
946943
///
944+
/// fn process_input<R: Reader>(r: R) {}
945+
///
947946
/// let mut stream = io::stdin();
948947
///
949948
/// // Only allow the function to process at most one kilobyte of input
@@ -953,8 +952,6 @@ unsafe fn slice_vec_capacity<'a, T>(v: &'a mut Vec<T>, start: uint, end: uint) -
953952
/// }
954953
///
955954
/// // 'stream' is still available for use here
956-
///
957-
/// # }
958955
/// ```
959956
pub struct RefReader<'a, R:'a> {
960957
/// The underlying reader which this is referencing
@@ -1269,12 +1266,11 @@ impl<'a> Writer for &'a mut (Writer+'a) {
12691266
/// # Example
12701267
///
12711268
/// ```
1272-
/// # fn main() {}
1273-
/// # fn process_input<R: Reader>(r: R) {}
1274-
/// # fn foo () {
12751269
/// use std::io::util::TeeReader;
12761270
/// use std::io::{stdin, ByRefWriter};
12771271
///
1272+
/// fn process_input<R: Reader>(r: R) {}
1273+
///
12781274
/// let mut output = Vec::new();
12791275
///
12801276
/// {
@@ -1285,7 +1281,6 @@ impl<'a> Writer for &'a mut (Writer+'a) {
12851281
/// }
12861282
///
12871283
/// println!("input processed: {:?}", output);
1288-
/// # }
12891284
/// ```
12901285
pub struct RefWriter<'a, W:'a> {
12911286
/// The underlying writer which this is referencing
@@ -1705,19 +1700,19 @@ pub enum FileType {
17051700
/// A structure used to describe metadata information about a file. This
17061701
/// structure is created through the `stat` method on a `Path`.
17071702
///
1708-
/// # Example
1703+
/// # Examples
1704+
///
1705+
/// ```no_run
1706+
/// # #![allow(unstable)]
1707+
///
1708+
/// use std::io::fs::PathExtensions;
17091709
///
1710-
/// ```
1711-
/// # use std::io::fs::PathExtensions;
1712-
/// # fn main() {}
1713-
/// # fn foo() {
17141710
/// let info = match Path::new("foo.txt").stat() {
17151711
/// Ok(stat) => stat,
17161712
/// Err(e) => panic!("couldn't read foo.txt: {}", e),
17171713
/// };
17181714
///
17191715
/// println!("byte size: {}", info.size);
1720-
/// # }
17211716
/// ```
17221717
#[derive(Copy, Hash)]
17231718
pub struct FileStat {

src/libstd/io/net/pipe.rs

-2
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,7 @@ impl UnixListener {
168168
/// # Example
169169
///
170170
/// ```
171-
/// # fn main() {}
172171
/// # fn foo() {
173-
/// # #![allow(unused_must_use)]
174172
/// use std::io::net::pipe::UnixListener;
175173
/// use std::io::{Listener, Acceptor};
176174
///

0 commit comments

Comments
 (0)