Skip to content
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

"Associated function" is used incorrectly #2559

Closed
AJTJ opened this issue Jan 7, 2021 · 4 comments
Closed

"Associated function" is used incorrectly #2559

AJTJ opened this issue Jan 7, 2021 · 4 comments
Labels
Milestone

Comments

@AJTJ
Copy link

AJTJ commented Jan 7, 2021

This language in chapter 19 is confusing for a new rust user:
However, associated functions that are part of traits don’t have a self parameter.

I'm wondering if it would be better to declare something along the lines of Associated functions that are not methods, do not have the self parameter, in order to help with understanding.

@AJTJ AJTJ changed the title Misleading and confusing sentence in chapter 19 Not an issue Jan 7, 2021
@AJTJ AJTJ changed the title Not an issue Ambiguous and confusing language Jan 7, 2021
@ibraheemdev
Copy link
Member

ibraheemdev commented Jan 7, 2021

referenced section in the book

The terminology used here is ambiguous:

However, associated functions that are part of traits don’t have a self parameter.

Because Animal::baby_name is an associated function rather than a method, and thus doesn’t have a self parameter...

The Rust reference clearly states:

Associated functions whose first parameter is named self are called methods

Therefore, methods are a subset of associated functions, so the terminology used here is actually wrong.

@jyn514
Copy link
Member

jyn514 commented Jan 7, 2021

Associated functions whose first parameter is named self are called methods

This seems wrong to me, or at least inconsistent with the implementation. https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.AssocItem.html#structfield.fn_has_self_parameter clearly allows associated items to have a self parameter.

@AJTJ
Copy link
Author

AJTJ commented Jan 9, 2021

To whom it may concern, I am happy to submit a PR for this fix.

@sudeepdino008
Copy link

@AJTJ Please do. In the section 5.3 in The Book:

Another useful feature of impl blocks is that we’re allowed to define functions within impl blocks that don’t take self as a parameter. These are called associated functions because they’re associated with the struct. They’re still functions, not methods, because they don’t have an instance of the struct to work with. You’ve already used the String::from associated function.

this is clearly wrong? Reading this gives an indication that associated functions are like static/class methods in other languages.

@carols10cents carols10cents added this to the ch19 milestone Jul 14, 2021
@carols10cents carols10cents changed the title Ambiguous and confusing language "Associated function" is used incorrectly Jul 18, 2021
carols10cents added a commit that referenced this issue Aug 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants