15
15
//! success and containing a value, and `Err(E)`, representing error
16
16
//! and containing an error value.
17
17
//!
18
- //! ~~~
18
+ //! ```
19
19
//! enum Result<T, E> {
20
20
//! Ok(T),
21
21
//! Err(E)
22
22
//! }
23
- //! ~~~
23
+ //! ```
24
24
//!
25
25
//! Functions return `Result` whenever errors are expected and
26
26
//! recoverable. In the `std` crate `Result` is most prominently used
29
29
//! A simple function returning `Result` might be
30
30
//! defined and used like so:
31
31
//!
32
- //! ~~~
32
+ //! ```
33
33
//! #[deriving(Show)]
34
34
//! enum Version { Version1, Version2 }
35
35
//!
53
53
//! println!("error parsing header: {}", e);
54
54
//! }
55
55
//! }
56
- //! ~~~
56
+ //! ```
57
57
//!
58
58
//! Pattern matching on `Result`s is clear and straightforward for
59
59
//! simple cases, but `Result` comes with some convenience methods
60
60
//! that make working it more succinct.
61
61
//!
62
- //! ~~~
62
+ //! ```
63
63
//! let good_result: Result<int, int> = Ok(10);
64
64
//! let bad_result: Result<int, int> = Err(10);
65
65
//!
79
79
//!
80
80
//! // Consume the result and return the contents with `unwrap`.
81
81
//! let final_awesome_result = good_result.ok().unwrap();
82
- //! ~~~
82
+ //! ```
83
83
//!
84
84
//! # Results must be used
85
85
//!
94
94
//! Consider the `write_line` method defined for I/O types
95
95
//! by the [`Writer`](../io/trait.Writer.html) trait:
96
96
//!
97
- //! ~~~
97
+ //! ```
98
98
//! use std::io::IoError;
99
99
//!
100
100
//! trait Writer {
101
101
//! fn write_line(&mut self, s: &str) -> Result<(), IoError>;
102
102
//! }
103
- //! ~~~
103
+ //! ```
104
104
//!
105
105
//! *Note: The actual definition of `Writer` uses `IoResult`, which
106
106
//! is just a synonym for `Result<T, IoError>`.*
109
109
//! fail. It's crucial to handle the error case, and *not* write
110
110
//! something like this:
111
111
//!
112
- //! ~~~ ignore
112
+ //! ```{. ignore}
113
113
//! use std::io::{File, Open, Write};
114
114
//!
115
115
//! let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write);
116
116
//! // If `write_line` errors, then we'll never know, because the return
117
117
//! // value is ignored.
118
118
//! file.write_line("important message");
119
119
//! drop(file);
120
- //! ~~~
120
+ //! ```
121
121
//!
122
122
//! If you *do* write that in Rust, the compiler will by give you a
123
123
//! warning (by default, controlled by the `unused_must_use` lint).
127
127
//! success with `expect`. This will fail if the write fails, proving
128
128
//! a marginally useful message indicating why:
129
129
//!
130
- //! ~~~ no_run
130
+ //! ```{. no_run}
131
131
//! use std::io::{File, Open, Write};
132
132
//!
133
133
//! let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write);
134
134
//! file.write_line("important message").ok().expect("failed to write message");
135
135
//! drop(file);
136
- //! ~~~
136
+ //! ```
137
137
//!
138
138
//! You might also simply assert success:
139
139
//!
140
- //! ~~~ no_run
140
+ //! ```{. no_run}
141
141
//! # use std::io::{File, Open, Write};
142
142
//!
143
143
//! # let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write);
144
144
//! assert!(file.write_line("important message").is_ok());
145
145
//! # drop(file);
146
- //! ~~~
146
+ //! ```
147
147
//!
148
148
//! Or propagate the error up the call stack with `try!`:
149
149
//!
150
- //! ~~~
150
+ //! ```
151
151
//! # use std::io::{File, Open, Write, IoError};
152
152
//! fn write_message() -> Result<(), IoError> {
153
153
//! let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write);
154
154
//! try!(file.write_line("important message"));
155
155
//! drop(file);
156
156
//! return Ok(());
157
157
//! }
158
- //! ~~~
158
+ //! ```
159
159
//!
160
160
//! # The `try!` macro
161
161
//!
166
166
//!
167
167
//! It replaces this:
168
168
//!
169
- //! ~~~
169
+ //! ```
170
170
//! use std::io::{File, Open, Write, IoError};
171
171
//!
172
172
//! struct Info {
188
188
//! }
189
189
//! return file.write_line(format!("rating: {}", info.rating).as_slice());
190
190
//! }
191
- //! ~~~
191
+ //! ```
192
192
//!
193
193
//! With this:
194
194
//!
195
- //! ~~~
195
+ //! ```
196
196
//! use std::io::{File, Open, Write, IoError};
197
197
//!
198
198
//! struct Info {
209
209
//! try!(file.write_line(format!("rating: {}", info.rating).as_slice()));
210
210
//! return Ok(());
211
211
//! }
212
- //! ~~~
212
+ //! ```
213
213
//!
214
214
//! *It's much nicer!*
215
215
//!
218
218
//! `Err` is returned early from the enclosing function. Its simple definition
219
219
//! makes it clear:
220
220
//!
221
- //! ~~~
221
+ //! ```
222
222
//! # #![feature(macro_rules)]
223
223
//! macro_rules! try(
224
224
//! ($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) })
225
225
//! )
226
226
//! # fn main() { }
227
- //! ~~~
227
+ //! ```
228
228
//!
229
229
//! `try!` is imported by the prelude, and is available everywhere.
230
230
//!
245
245
//!
246
246
//! Converting to an `Option` with `ok()` to handle an error:
247
247
//!
248
- //! ~~~
248
+ //! ```
249
249
//! use std::io::Timer;
250
250
//! let mut t = Timer::new().ok().expect("failed to create timer!");
251
- //! ~~~
251
+ //! ```
252
252
//!
253
253
//! # `Result` vs. `fail!`
254
254
//!
@@ -440,12 +440,12 @@ impl<T, E> Result<T, E> {
440
440
///
441
441
/// This function can be used to compose the results of two functions.
442
442
///
443
- /// # Examples
443
+ /// # Example
444
444
///
445
445
/// Sum the lines of a buffer by mapping strings to numbers,
446
446
/// ignoring I/O and parse errors:
447
447
///
448
- /// ~~~
448
+ /// ```
449
449
/// use std::io::{BufReader, IoResult};
450
450
///
451
451
/// let buffer = "1\n2\n3\n4\n";
@@ -464,7 +464,7 @@ impl<T, E> Result<T, E> {
464
464
/// }
465
465
///
466
466
/// assert!(sum == 10);
467
- /// ~~~
467
+ /// ```
468
468
#[ inline]
469
469
#[ unstable = "waiting for unboxed closures" ]
470
470
pub fn map < U > ( self , op: |T | -> U ) -> Result < U , E > {
0 commit comments