Skip to content

Problem in "The Rust Programming Language" 5.7 "Error Handling" #31266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
oxeb opened this issue Jan 28, 2016 · 1 comment
Closed

Problem in "The Rust Programming Language" 5.7 "Error Handling" #31266

oxeb opened this issue Jan 28, 2016 · 1 comment

Comments

@oxeb
Copy link

oxeb commented Jan 28, 2016

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.

@steveklabnik
Copy link
Member

Yes, you're right here. thank you!

steveklabnik added a commit to steveklabnik/rust that referenced this issue Jan 29, 2016
These are free functions in the text, but methods in the standard
library.

Fixes rust-lang#31266
Manishearth added a commit to Manishearth/rust that referenced this issue Jan 30, 2016
These are free functions in the text, but methods in the standard
library.

Fixes rust-lang#31266
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants