From 5ab3058569245171a44283fddee6c119a31765b7 Mon Sep 17 00:00:00 2001 From: David Szotten Date: Sat, 19 Sep 2015 12:06:36 +0100 Subject: [PATCH] change back to anchors; divs break md --- src/doc/trpl/error-handling.md | 36 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/doc/trpl/error-handling.md b/src/doc/trpl/error-handling.md index 18d2fae0d754..18ce93ea06a6 100644 --- a/src/doc/trpl/error-handling.md +++ b/src/doc/trpl/error-handling.md @@ -87,7 +87,8 @@ thread '
' panicked at 'Invalid number: 11', src/bin/panic-simple.rs:5 Here's another example that is slightly less contrived. A program that accepts an integer as an argument, doubles it and prints it. -
+ + ```rust,should_panic use std::env; @@ -98,7 +99,6 @@ fn main() { println!("{}", 2 * n); } ``` -
If you give this program zero arguments (error 1) or if the first argument isn't an integer (error 2), the program will panic just like in the first @@ -139,7 +139,8 @@ system is an important concept because it will cause the compiler to force the programmer to handle that absence. Let's take a look at an example that tries to find a character in a string: -
+ + ```rust // Searches `haystack` for the Unicode character `needle`. If one is found, the // byte offset of the character is returned. Otherwise, `None` is returned. @@ -152,7 +153,6 @@ fn find(haystack: &str, needle: char) -> Option { None } ``` -
Notice that when this function finds a matching character, it doen't just return the `offset`. Instead, it returns `Some(offset)`. `Some` is a variant or @@ -186,7 +186,8 @@ But wait, what about `unwrap` used in [`unwrap-double`](#code-unwrap-double)? There was no case analysis there! Instead, the case analysis was put inside the `unwrap` method for you. You could define it yourself if you want: -
+ + ```rust enum Option { None, @@ -203,7 +204,6 @@ impl Option { } } ``` -
The `unwrap` method *abstracts away the case analysis*. This is precisely the thing that makes `unwrap` ergonomic to use. Unfortunately, that `panic!` means that @@ -253,7 +253,8 @@ option is `None`, in which case, just return `None`. Rust has parametric polymorphism, so it is very easy to define a combinator that abstracts this pattern: - Indeed, `map` is [defined as a method][2] on `Option` in the standard library. @@ -394,14 +394,14 @@ remove choices because they will panic if `Option` is `None`. The `Result` type is also [defined in the standard library][6]: -
+ + ```rust enum Result { Ok(T), Err(E), } ``` -
The `Result` type is a richer version of `Option`. Instead of expressing the possibility of *absence* like `Option` does, `Result` expresses the possibility @@ -672,7 +672,8 @@ with both an `Option` and a `Result`, the solution is *usually* to convert the (from `env::args()`) means the user didn't invoke the program correctly. We could just use a `String` to describe the error. Let's try: -
+ + ```rust use std::env; @@ -689,7 +690,6 @@ fn main() { } } ``` -
There are a couple new things in this example. The first is the use of the [`Option::ok_or`](../std/option/enum.Option.html#method.ok_or) @@ -906,7 +906,8 @@ seen above. Here is a simplified definition of a `try!` macro: -
+ + ```rust macro_rules! try { ($e:expr) => (match $e { @@ -915,7 +916,6 @@ macro_rules! try { }); } ``` -
(The [real definition](../std/macro.try!.html) is a bit more sophisticated. We will address that later.) @@ -1168,13 +1168,13 @@ The `std::convert::From` trait is [defined in the standard library](../std/convert/trait.From.html): -
+ + ```rust trait From { fn from(T) -> Self; } ``` -
Deliciously simple, yes? `From` is very useful because it gives us a generic way to talk about conversion *from* a particular type `T` to some other type @@ -1250,7 +1250,8 @@ macro_rules! try { This is not its real definition. Its real definition is [in the standard library](../std/macro.try!.html): -
+ + ```rust macro_rules! try { ($e:expr) => (match $e { @@ -1259,7 +1260,6 @@ macro_rules! try { }); } ``` -
There's one tiny but powerful change: the error value is passed through `From::from`. This makes the `try!` macro a lot more powerful because it gives