-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Soup up 'method syntax' chapter of the Book #21108
Conversation
r? @pcwalton (rust_highfive has picked a reviewer for you, use r? to override) |
## Chaining method calls | ||
|
||
So, now we know how to call a method, such as `foo.bar()`. But what about our | ||
original example, `foo.bar().baz()`? This is called 'method chaning', and we |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"method chaining"
Thanks @thommay |
Sorry that was a bit of a drive-by review :) |
No sorries needed, I appreciate it very much! |
|
||
We just say we're returning a `Circle`. With this, we can grow a new circle | ||
that's twice as big as the old one. But what if we didn't want to make a new | ||
Circle, but change the old one? There's a pattern where this is commonly used: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sentence seems a little odd starting with "But" and then also having a conjunction "but"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the second one should be 'and'
d693938
to
82f43c7
Compare
} | ||
|
||
impl Circle { | ||
fn x(&mut self, x: f64) -> &mut Circle { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be more convenient for the caller if the method takes and returns self by value?
eg: fn x(mut self, x: f64) -> Circle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep reading ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're referring to the "Advanced Builder Pattern" section then it's missing.
If not then I don't know what I missed.
let circle: Circle = Circle::new().radius(5.0).coordinate(10.0); // Methods are by-value
seems a lot more usable to me than
let mut circle_builder = CircleBuilder::new();
circle_builder.radius(5.0).coordinate(10.0); // Methods are by-reference
let circle: Circle = circle_builder.finalise();
Edit: Or even the simplified case without an extra builder type:
let mut circle: Circle = Circle::new();
circle.radius(5.0).coordinate(10.0); // Methods are by-reference
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was, since the consuming form was what I was thinking of.
I think you're right, after seeing that code. Will change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that builders by reference are currently preferred, but there are definitely tradeoffs both ways. This is meant to be a fairly trivial example, so the by-reference may not make much sense, but I've found that by-reference is almost always what you want for some serious configuration going on as you don't have to move it around all over the place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish that we had pubished the guidelines officially so I could link to them from here...
I did some significant editing here. What do you think @alexcrichton ? |
|
||
let c = cb.coordinate(10.0) | ||
.radius(5.0); | ||
.finalize(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this compile? The finalize
method consumes the builder but the previous methods are just returning references (and the builder doesn't have derive(Copy)
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i ran rustdoc --test and it passed...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like there's another error as well (the semicolon after the call to radius
)
@alexcrichton addressed. |
``` | ||
# struct Circle; | ||
# impl Circle { | ||
fn finalize(self) -> Circle { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this (and the below paragraph need to be udpated)
Just a small nit, otherwise r=me, thanks! |
Fixes #16969