1515//! success and containing a value, and `Err(E)`, representing error
1616//! and containing an error value.
1717//!
18- //! ~~~
18+ //! ```
1919//! enum Result<T, E> {
2020//! Ok(T),
2121//! Err(E)
2222//! }
23- //! ~~~
23+ //! ```
2424//!
2525//! Functions return `Result` whenever errors are expected and
2626//! recoverable. In the `std` crate `Result` is most prominently used
2929//! A simple function returning `Result` might be
3030//! defined and used like so:
3131//!
32- //! ~~~
32+ //! ```
3333//! #[deriving(Show)]
3434//! enum Version { Version1, Version2 }
3535//!
5353//! println!("error parsing header: {}", e);
5454//! }
5555//! }
56- //! ~~~
56+ //! ```
5757//!
5858//! Pattern matching on `Result`s is clear and straightforward for
5959//! simple cases, but `Result` comes with some convenience methods
6060//! that make working it more succinct.
6161//!
62- //! ~~~
62+ //! ```
6363//! let good_result: Result<int, int> = Ok(10);
6464//! let bad_result: Result<int, int> = Err(10);
6565//!
7979//!
8080//! // Consume the result and return the contents with `unwrap`.
8181//! let final_awesome_result = good_result.ok().unwrap();
82- //! ~~~
82+ //! ```
8383//!
8484//! # Results must be used
8585//!
9494//! Consider the `write_line` method defined for I/O types
9595//! by the [`Writer`](../io/trait.Writer.html) trait:
9696//!
97- //! ~~~
97+ //! ```
9898//! use std::io::IoError;
9999//!
100100//! trait Writer {
101101//! fn write_line(&mut self, s: &str) -> Result<(), IoError>;
102102//! }
103- //! ~~~
103+ //! ```
104104//!
105105//! *Note: The actual definition of `Writer` uses `IoResult`, which
106106//! is just a synonym for `Result<T, IoError>`.*
109109//! fail. It's crucial to handle the error case, and *not* write
110110//! something like this:
111111//!
112- //! ~~~ ignore
112+ //! ```{. ignore}
113113//! use std::io::{File, Open, Write};
114114//!
115115//! let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write);
116116//! // If `write_line` errors, then we'll never know, because the return
117117//! // value is ignored.
118118//! file.write_line("important message");
119119//! drop(file);
120- //! ~~~
120+ //! ```
121121//!
122122//! If you *do* write that in Rust, the compiler will by give you a
123123//! warning (by default, controlled by the `unused_must_use` lint).
127127//! success with `expect`. This will fail if the write fails, proving
128128//! a marginally useful message indicating why:
129129//!
130- //! ~~~ no_run
130+ //! ```{. no_run}
131131//! use std::io::{File, Open, Write};
132132//!
133133//! let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write);
134134//! file.write_line("important message").ok().expect("failed to write message");
135135//! drop(file);
136- //! ~~~
136+ //! ```
137137//!
138138//! You might also simply assert success:
139139//!
140- //! ~~~ no_run
140+ //! ```{. no_run}
141141//! # use std::io::{File, Open, Write};
142142//!
143143//! # let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write);
144144//! assert!(file.write_line("important message").is_ok());
145145//! # drop(file);
146- //! ~~~
146+ //! ```
147147//!
148148//! Or propagate the error up the call stack with `try!`:
149149//!
150- //! ~~~
150+ //! ```
151151//! # use std::io::{File, Open, Write, IoError};
152152//! fn write_message() -> Result<(), IoError> {
153153//! let mut file = File::open_mode(&Path::new("valuable_data.txt"), Open, Write);
154154//! try!(file.write_line("important message"));
155155//! drop(file);
156156//! return Ok(());
157157//! }
158- //! ~~~
158+ //! ```
159159//!
160160//! # The `try!` macro
161161//!
166166//!
167167//! It replaces this:
168168//!
169- //! ~~~
169+ //! ```
170170//! use std::io::{File, Open, Write, IoError};
171171//!
172172//! struct Info {
188188//! }
189189//! return file.write_line(format!("rating: {}", info.rating).as_slice());
190190//! }
191- //! ~~~
191+ //! ```
192192//!
193193//! With this:
194194//!
195- //! ~~~
195+ //! ```
196196//! use std::io::{File, Open, Write, IoError};
197197//!
198198//! struct Info {
209209//! try!(file.write_line(format!("rating: {}", info.rating).as_slice()));
210210//! return Ok(());
211211//! }
212- //! ~~~
212+ //! ```
213213//!
214214//! *It's much nicer!*
215215//!
218218//! `Err` is returned early from the enclosing function. Its simple definition
219219//! makes it clear:
220220//!
221- //! ~~~
221+ //! ```
222222//! # #![feature(macro_rules)]
223223//! macro_rules! try(
224224//! ($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) })
225225//! )
226226//! # fn main() { }
227- //! ~~~
227+ //! ```
228228//!
229229//! `try!` is imported by the prelude, and is available everywhere.
230230//!
245245//!
246246//! Converting to an `Option` with `ok()` to handle an error:
247247//!
248- //! ~~~
248+ //! ```
249249//! use std::io::Timer;
250250//! let mut t = Timer::new().ok().expect("failed to create timer!");
251- //! ~~~
251+ //! ```
252252//!
253253//! # `Result` vs. `fail!`
254254//!
@@ -440,12 +440,12 @@ impl<T, E> Result<T, E> {
440440 ///
441441 /// This function can be used to compose the results of two functions.
442442 ///
443- /// # Examples
443+ /// # Example
444444 ///
445445 /// Sum the lines of a buffer by mapping strings to numbers,
446446 /// ignoring I/O and parse errors:
447447 ///
448- /// ~~~
448+ /// ```
449449 /// use std::io::{BufReader, IoResult};
450450 ///
451451 /// let buffer = "1\n2\n3\n4\n";
@@ -464,7 +464,7 @@ impl<T, E> Result<T, E> {
464464 /// }
465465 ///
466466 /// assert!(sum == 10);
467- /// ~~~
467+ /// ```
468468 #[ inline]
469469 #[ unstable = "waiting for unboxed closures" ]
470470 pub fn map < U > ( self , op: |T | -> U ) -> Result < U , E > {
0 commit comments