Skip to content

Commit 9f1739a

Browse files
committed
std: Fix all code examples
1 parent 6c9c045 commit 9f1739a

23 files changed

+147
-90
lines changed

src/libstd/c_str.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ let my_string = "Hello, world!";
5151
let my_c_string = my_string.to_c_str();
5252
my_c_string.with_ref(|c_buffer| {
5353
unsafe { puts(c_buffer); }
54-
})
54+
});
5555
5656
// Don't save off the allocation of the C string, the `c_buffer` will be
5757
// deallocated when this block returns!
5858
my_string.with_c_str(|c_buffer| {
5959
unsafe { puts(c_buffer); }
60-
})
60+
});
6161
```
6262
6363
*/
@@ -216,7 +216,11 @@ pub trait ToCStr {
216216
/// # Example
217217
///
218218
/// ```rust
219-
/// let s = "PATH".with_c_str(|path| libc::getenv(path))
219+
/// use std::libc;
220+
///
221+
/// let s = "PATH".with_c_str(|path| unsafe {
222+
/// libc::getenv(path)
223+
/// });
220224
/// ```
221225
///
222226
/// # Failure

src/libstd/cast.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ pub unsafe fn bump_box_refcount<T>(t: @T) { forget(t); }
4949
* # Example
5050
*
5151
* ```rust
52-
* let v: &[u8] = transmute("L");
52+
* use std::cast;
53+
*
54+
* let v: &[u8] = unsafe { cast::transmute("L") };
5355
* assert!(v == [76u8]);
5456
* ```
5557
*/

src/libstd/comm/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
//!
5858
//! # Example
5959
//!
60-
//! ```rust
60+
//! ```rust,should_fail
6161
//! // Create a simple streaming channel
6262
//! let (port, chan) = Chan::new();
6363
//! do spawn {
@@ -81,7 +81,7 @@
8181
//!
8282
//! // The call to recv() will fail!() because the channel has already hung
8383
//! // up (or been deallocated)
84-
//! let (port, chan) = Chan::new();
84+
//! let (port, chan) = Chan::<int>::new();
8585
//! drop(chan);
8686
//! port.recv();
8787
//! ```

src/libstd/comm/select.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
//!
2626
//! # Example
2727
//!
28-
//! ```rust
28+
//! ```rust,notest
2929
//! let (mut p1, c1) = Chan::new();
3030
//! let (mut p2, c2) = Chan::new();
3131
//!
@@ -40,6 +40,7 @@
4040
//! assert_eq!(val, 2);
4141
//! }
4242
//! )
43+
//! ```
4344
4445
#[allow(dead_code)];
4546

src/libstd/condition.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ A condition is declared through the `condition!` macro provided by the compiler:
2323
condition! {
2424
pub my_error: int -> ~str;
2525
}
26-
```
26+
# fn main() {}
27+
```
2728
2829
This macro declares an inner module called `my_error` with one static variable,
2930
`cond` that is a static `Condition` instance. To help understand what the other
3031
parameters are used for, an example usage of this condition would be:
3132
3233
```rust
34+
# condition! { pub my_error: int -> ~str; }
35+
# fn main() {
36+
3337
my_error::cond.trap(|raised_int| {
3438
3539
// the condition `my_error` was raised on, and the value it raised is stored
@@ -51,6 +55,8 @@ my_error::cond.trap(|raised_int| {
5155
println(my_error::cond.raise(4)); // prints "oh well"
5256
5357
})
58+
59+
# }
5460
```
5561
5662
Condition handling is useful in cases where propagating errors is either to
@@ -99,10 +105,12 @@ impl<T, U> Condition<T, U> {
99105
/// ```rust
100106
/// condition! { my_error: int -> int; }
101107
///
108+
/// # fn main() {
102109
/// let trap = my_error::cond.trap(|error| error + 3);
103110
///
104111
/// // use `trap`'s inside method to register the handler and then run a
105112
/// // block of code with the handler registered
113+
/// # }
106114
/// ```
107115
pub fn trap<'a>(&'a self, h: 'a |T| -> U) -> Trap<'a, T, U> {
108116
let h: Closure = unsafe { ::cast::transmute(h) };
@@ -176,10 +184,12 @@ impl<'a, T, U> Trap<'a, T, U> {
176184
/// ```rust
177185
/// condition! { my_error: int -> int; }
178186
///
187+
/// # fn main() {
179188
/// let result = my_error::cond.trap(|error| error + 3).inside(|| {
180189
/// my_error::cond.raise(4)
181190
/// });
182191
/// assert_eq!(result, 7);
192+
/// # }
183193
/// ```
184194
pub fn inside<V>(&self, inner: 'a || -> V) -> V {
185195
let _g = Guard { cond: self.cond };

src/libstd/fmt/mod.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ arguments directly while performing minimal allocations.
3434
Some examples of the `format!` extension are:
3535
3636
```rust
37-
format!("Hello") // => ~"Hello"
38-
format!("Hello, {:s}!", "world") // => ~"Hello, world!"
39-
format!("The number is {:d}", 1) // => ~"The number is 1"
40-
format!("{:?}", ~[3, 4]) // => ~"~[3, 4]"
41-
format!("{value}", value=4) // => ~"4"
42-
format!("{} {}", 1, 2) // => ~"1 2"
37+
format!("Hello"); // => ~"Hello"
38+
format!("Hello, {:s}!", "world"); // => ~"Hello, world!"
39+
format!("The number is {:d}", 1); // => ~"The number is 1"
40+
format!("{:?}", ~[3, 4]); // => ~"~[3, 4]"
41+
format!("{value}", value=4); // => ~"4"
42+
format!("{} {}", 1, 2); // => ~"1 2"
4343
```
4444
4545
From these, you can see that the first argument is a format string. It is
@@ -62,7 +62,7 @@ iterator over the argument. Each time a "next argument" specifier is seen, the
6262
iterator advances. This leads to behavior like this:
6363
6464
```rust
65-
format!("{1} {} {0} {}", 1, 2) // => ~"2 1 1 2"
65+
format!("{1} {} {0} {}", 1, 2); // => ~"2 1 1 2"
6666
```
6767
6868
The internal iterator over the argument has not been advanced by the time the
@@ -89,9 +89,9 @@ identifier '=' expression
8989
For example, the following `format!` expressions all use named argument:
9090
9191
```rust
92-
format!("{argument}", argument = "test") // => ~"test"
93-
format!("{name} {}", 1, name = 2) // => ~"2 1"
94-
format!("{a:s} {c:d} {b:?}", a="a", b=(), c=3) // => ~"a 3 ()"
92+
format!("{argument}", argument = "test"); // => ~"test"
93+
format!("{name} {}", 1, name = 2); // => ~"2 1"
94+
format!("{a:s} {c:d} {b:?}", a="a", b=(), c=3); // => ~"a 3 ()"
9595
```
9696
9797
It is illegal to put positional parameters (those without names) after arguments
@@ -160,7 +160,11 @@ When implementing a format trait for your own time, you will have to implement a
160160
method of the signature:
161161
162162
```rust
163+
# use std;
164+
# struct T;
165+
# trait SomeName<T> {
163166
fn fmt(value: &T, f: &mut std::fmt::Formatter);
167+
# }
164168
```
165169
166170
Your type will be passed by-reference in `value`, and then the function should
@@ -218,7 +222,7 @@ fn main() {
218222
There are a number of related macros in the `format!` family. The ones that are
219223
currently implemented are:
220224
221-
```rust
225+
```rust,notest
222226
format! // described above
223227
write! // first argument is a &mut io::Writer, the destination
224228
writeln! // same as write but appends a newline
@@ -261,9 +265,13 @@ references information on the stack. Under the hood, all of
261265
the related macros are implemented in terms of this. First
262266
off, some example usage is:
263267
264-
```rust
268+
```rust,ignore
265269
use std::fmt;
266270
271+
# fn lol<T>() -> T { fail!() }
272+
# let my_writer: &mut ::std::io::Writer = lol();
273+
# let my_fn: fn(&fmt::Arguments) = lol();
274+
267275
format_args!(fmt::format, "this returns {}", "~str");
268276
format_args!(|args| { fmt::write(my_writer, args) }, "some {}", "args");
269277
format_args!(my_fn, "format {}", "string");
@@ -305,7 +313,7 @@ to reference the string value of the argument which was selected upon. As an
305313
example:
306314
307315
```rust
308-
format!("{0, select, other{#}}", "hello") // => ~"hello"
316+
format!("{0, select, other{#}}", "hello"); // => ~"hello"
309317
```
310318
311319
This example is the equivalent of `{0:s}` essentially.
@@ -585,7 +593,9 @@ pub trait Float { fn fmt(&Self, &mut Formatter); }
585593
///
586594
/// ```rust
587595
/// use std::fmt;
588-
/// let w: &mut io::Writer = ...;
596+
/// use std::io;
597+
///
598+
/// let w = &mut io::stdout() as &mut io::Writer;
589599
/// format_args!(|args| { fmt::write(w, args) }, "Hello, {}!", "world");
590600
/// ```
591601
pub fn write(output: &mut io::Writer, args: &Arguments) {
@@ -650,8 +660,9 @@ pub unsafe fn write_unsafe(output: &mut io::Writer,
650660
///
651661
/// ```rust
652662
/// use std::fmt;
663+
///
653664
/// let s = format_args!(fmt::format, "Hello, {}!", "world");
654-
/// assert_eq!(s, "Hello, world!");
665+
/// assert_eq!(s, ~"Hello, world!");
655666
/// ```
656667
pub fn format(args: &Arguments) -> ~str {
657668
unsafe { format_unsafe(args.fmt, args.args) }

src/libstd/io/mod.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ Some examples of obvious things you might want to do
2626
* Read lines from stdin
2727
2828
```rust
29+
use std::io::buffered::BufferedReader;
30+
use std::io::stdin;
31+
2932
let mut stdin = BufferedReader::new(stdin());
3033
for line in stdin.lines() {
3134
print(line);
@@ -35,19 +38,26 @@ Some examples of obvious things you might want to do
3538
* Read a complete file
3639
3740
```rust
41+
use std::io::File;
42+
3843
let contents = File::open(&Path::new("message.txt")).read_to_end();
3944
```
4045
4146
* Write a line to a file
4247
4348
```rust
49+
use std::io::File;
50+
4451
let mut file = File::create(&Path::new("message.txt"));
4552
file.write(bytes!("hello, file!\n"));
4653
```
4754
4855
* Iterate over the lines of a file
4956
5057
```rust
58+
use std::io::buffered::BufferedReader;
59+
use std::io::File;
60+
5161
let path = Path::new("message.txt");
5262
let mut file = BufferedReader::new(File::open(&path));
5363
for line in file.lines() {
@@ -58,6 +68,9 @@ Some examples of obvious things you might want to do
5868
* Pull the lines of a file into a vector of strings
5969
6070
```rust
71+
use std::io::buffered::BufferedReader;
72+
use std::io::File;
73+
6174
let path = Path::new("message.txt");
6275
let mut file = BufferedReader::new(File::open(&path));
6376
let lines: ~[~str] = file.lines().collect();
@@ -67,7 +80,10 @@ Some examples of obvious things you might want to do
6780
XXX This needs more improvement: TcpStream constructor taking &str,
6881
`write_str` and `write_line` methods.
6982
70-
```rust
83+
```rust,ignore
84+
use std::io::net::ip::SocketAddr;
85+
use std::io::net::tcp::TcpStream;
86+
7187
let addr = from_str::<SocketAddr>("127.0.0.1:8080").unwrap();
7288
let mut socket = TcpStream::connect(addr).unwrap();
7389
socket.write(bytes!("GET / HTTP/1.0\n\n"));

src/libstd/io/signal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ pub enum Signum {
6060
///
6161
/// # Example
6262
///
63-
/// ```rust
63+
/// ```rust,ignore
6464
/// use std::io::signal::{Listener, Interrupt};
6565
///
6666
/// let mut listener = Listener::new();
67-
/// listener.register(signal::Interrupt);
67+
/// listener.register(Interrupt);
6868
///
6969
/// do spawn {
7070
/// loop {

src/libstd/io/timer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ and create ports which will receive notifications after a period of time.
1717
1818
# Example
1919
20-
```rust
20+
```rust,ignore
2121
2222
use std::io::Timer;
2323

0 commit comments

Comments
 (0)