Description
Under the heading "Composing Option values" in Section 5.7 of the Rust Programming Language at https://doc.rust-lang.org/book/error-handling.html, a function is defined:
fn map<F, T, A>(option: Option<T>, f: F) -> Option<A> where F: FnOnce(T) -> A {
A later example invokes appears to invoke this function:
fn extension(file_name: &str) -> Option<&str> {
find(file_name, '.').map(|i| &file_name[i+1..])
}
This compiles but I believe it is not using this version of map presented in the book, but rather it is using the one from the standard library. If you rename the function to z_map to distinguish them you get the following message:
<anon>:18:26: 18:54 error: no method named `z_map` found for type `core::option::Option<usize>` in the current scope
<anon>:18 find(file_name, '.').z_map(|i| &file_name[i+1..])
I believe that this an error in the book because the function presented does not use self, &self, or &mut self as the first parameter and thus cannot use dot syntax for calling map. However, it may be intended to call the real map function and not the one in the prior example. If that is the case I find this area confusing and would suggest that this is changed or clarified. I think changing the name of the example function would be sufficient personally.
Later in the same area of the book there is another similar example function:
fn unwrap_or<T>(option: Option<T>, default: T) -> T {
and then for the example following it, a note:
(Note that unwrap_or is defined as a method on Option in the standard library, so we use that here instead of the free-standing function we defined above.
That would be a great inclusion too.