You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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<Option<&str>>`.
362
+
The `map` function here wraps the value returned by the `extension` function
363
+
inside an `Option<_>` and since the `extension` function itself returns an
364
+
`Option<&str>` the expression `file_name(file_path).map(|x| extension(x))`
365
+
actually returns an `Option<Option<&str>>`.
363
366
364
-
But since `file_path_ext` just returns `Option<&str>` (and not `Option<Option<&str>>`) we get a compilation error.
367
+
But since `file_path_ext` just returns `Option<&str>` (and not
368
+
`Option<Option<&str>>`) we get a compilation error.
365
369
366
-
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<_>`.
370
+
The result of the function taken by map as input is *always*[rewrapped with
371
+
`Some`](#code-option-map). Instead, we need something like `map`, but which
372
+
allows the caller to return a `Option<_>` directly without wrapping it in
373
+
another `Option<_>`.
367
374
368
375
Its generic implementation is even simpler than `map`:
Side note: Since `and_then` essentially works like `map` but returns an `Option<_>` instead of an `Option<Option<_>>` it is known as `flatmap` in some other languages.
397
+
Side note: Since `and_then` essentially works like `map` but returns an
398
+
`Option<_>` instead of an `Option<Option<_>>` it is known as `flatmap` in some
399
+
other languages.
391
400
392
401
The `Option` type has many other combinators [defined in the standard
393
402
library][5]. It is a good idea to skim this list and familiarize
0 commit comments