From 25d068e36ed2f4c1b526185fbb2763c38616a10f Mon Sep 17 00:00:00 2001 From: Sandeep Datta Date: Sun, 17 Jan 2016 14:08:01 +0530 Subject: [PATCH 1/3] Showed the difference between `map` and `and_then` with an example. --- src/doc/book/error-handling.md | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/doc/book/error-handling.md b/src/doc/book/error-handling.md index 9b1d16170b97f..b5c0eabba9de7 100644 --- a/src/doc/book/error-handling.md +++ b/src/doc/book/error-handling.md @@ -351,11 +351,21 @@ fn file_name(file_path: &str) -> Option<&str> { ``` You might think that we could use the `map` combinator to reduce the case -analysis, but its type doesn't quite fit. Namely, `map` takes a function that -does something only with the inner value. The result of that function is then -*always* [rewrapped with `Some`](#code-option-map). Instead, we need something -like `map`, but which allows the caller to return another `Option`. Its generic -implementation is even simpler than `map`: +analysis, but its type doesn't quite fit... + +```rust +fn file_path_ext(file_path: &str) -> Option<&str> { + file_name(file_path).map(|x| extension(x)) //Compilation error +} +``` + +The `map` function here wraps the value returned by the `extension` function inside an `Option<_>` and since the `extension` function itself returns an `Option<&str>` the expression `file_name(file_path).map(|x| extension(x))` actually returns an `Option>`. + +But since `file_path_ext` just returns `Option<&str>` (and not `Option>`) we get a compilation error. + +The result of the function taken by map as input is *always* [rewrapped with `Some`](#code-option-map). Instead, we need something like `map`, but which allows the caller to return a `Option<_>` directly without wrapping it in another `Option<_>`. + +Its generic implementation is even simpler than `map`: ```rust fn and_then(option: Option, f: F) -> Option @@ -377,6 +387,8 @@ fn file_path_ext(file_path: &str) -> Option<&str> { } ``` +Side note: Since `and_then` essentially works like `map` but returns an `Option<_>` instead of an `Option>` it is known as `flatmap` in some other languages. + The `Option` type has many other combinators [defined in the standard library][5]. It is a good idea to skim this list and familiarize yourself with what's available—they can often reduce case analysis From 5f20143ccfd1a74a8a104ecac33f777ade12992f Mon Sep 17 00:00:00 2001 From: Sandeep Datta Date: Wed, 20 Jan 2016 19:48:00 +0530 Subject: [PATCH 2/3] Fixed line wrapping. --- src/doc/book/error-handling.md | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/doc/book/error-handling.md b/src/doc/book/error-handling.md index b5c0eabba9de7..d4df6a813bc80 100644 --- a/src/doc/book/error-handling.md +++ b/src/doc/book/error-handling.md @@ -359,11 +359,18 @@ fn file_path_ext(file_path: &str) -> Option<&str> { } ``` -The `map` function here wraps the value returned by the `extension` function inside an `Option<_>` and since the `extension` function itself returns an `Option<&str>` the expression `file_name(file_path).map(|x| extension(x))` actually returns an `Option>`. +The `map` function here wraps the value returned by the `extension` function +inside an `Option<_>` and since the `extension` function itself returns an +`Option<&str>` the expression `file_name(file_path).map(|x| extension(x))` +actually returns an `Option>`. -But since `file_path_ext` just returns `Option<&str>` (and not `Option>`) we get a compilation error. +But since `file_path_ext` just returns `Option<&str>` (and not +`Option>`) we get a compilation error. -The result of the function taken by map as input is *always* [rewrapped with `Some`](#code-option-map). Instead, we need something like `map`, but which allows the caller to return a `Option<_>` directly without wrapping it in another `Option<_>`. +The result of the function taken by map as input is *always* [rewrapped with +`Some`](#code-option-map). Instead, we need something like `map`, but which +allows the caller to return a `Option<_>` directly without wrapping it in +another `Option<_>`. Its generic implementation is even simpler than `map`: @@ -387,7 +394,9 @@ fn file_path_ext(file_path: &str) -> Option<&str> { } ``` -Side note: Since `and_then` essentially works like `map` but returns an `Option<_>` instead of an `Option>` it is known as `flatmap` in some other languages. +Side note: Since `and_then` essentially works like `map` but returns an +`Option<_>` instead of an `Option>` it is known as `flatmap` in some +other languages. The `Option` type has many other combinators [defined in the standard library][5]. It is a good idea to skim this list and familiarize From 0922d7e68f3186aa57751af26f3342b1837a5140 Mon Sep 17 00:00:00 2001 From: Sandeep Datta Date: Sat, 30 Jan 2016 13:43:02 +0530 Subject: [PATCH 3/3] Ignoring demo code with compilation error. --- src/doc/book/error-handling.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/book/error-handling.md b/src/doc/book/error-handling.md index d4df6a813bc80..cf78a21d26fd5 100644 --- a/src/doc/book/error-handling.md +++ b/src/doc/book/error-handling.md @@ -353,7 +353,7 @@ fn file_name(file_path: &str) -> Option<&str> { You might think that we could use the `map` combinator to reduce the case analysis, but its type doesn't quite fit... -```rust +```rust,ignore fn file_path_ext(file_path: &str) -> Option<&str> { file_name(file_path).map(|x| extension(x)) //Compilation error }