Skip to content

Commit ca89cfb

Browse files
committed
auto merge of #16255 : steveklabnik/rust/guide_methods, r=nikomatsakis
Shifting some things around here, as I think this is a better order.
2 parents 86decf6 + 50ffe0c commit ca89cfb

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

src/doc/guide.md

+88
Original file line numberDiff line numberDiff line change
@@ -3614,6 +3614,94 @@ guide](http://doc.rust-lang.org/guide-pointers.html#rc-and-arc).
36143614

36153615
# Patterns
36163616

3617+
# Method Syntax
3618+
3619+
Functions are great, but if you want to call a bunch of them on some data, it
3620+
can be awkward. Consider this code:
3621+
3622+
```{rust,ignore}
3623+
baz(bar(foo(x)));
3624+
```
3625+
3626+
We would read this left-to right, and so we see 'baz bar foo.' But this isn't the
3627+
order that the functions would get called in, that's inside-out: 'foo bar baz.'
3628+
Wouldn't it be nice if we could do this instead?
3629+
3630+
```{rust,ignore}
3631+
x.foo().bar().baz();
3632+
```
3633+
3634+
Luckily, as you may have guessed with the leading question, you can! Rust provides
3635+
the ability to use this **method call syntax** via the `impl` keyword.
3636+
3637+
Here's how it works:
3638+
3639+
```
3640+
struct Circle {
3641+
x: f64,
3642+
y: f64,
3643+
radius: f64,
3644+
}
3645+
3646+
impl Circle {
3647+
fn area(&self) -> f64 {
3648+
std::f64::consts::PI * (self.radius * self.radius)
3649+
}
3650+
}
3651+
3652+
fn main() {
3653+
let c = Circle { x: 0.0, y: 0.0, radius: 2.0 };
3654+
println!("{}", c.area());
3655+
}
3656+
```
3657+
3658+
This will print `12.566371`.
3659+
3660+
We've made a struct that represents a circle. We then write an `impl` block,
3661+
and inside it, define a method, `area`. Methods take a special first
3662+
parameter, `&self`. There are three variants: `self`, `&self`, and `&mut self`.
3663+
You can think of this first parameter as being the `x` in `x.foo()`. The three
3664+
variants correspond to the three kinds of thing `x` could be: `self` if it's
3665+
just a value on the stack, `&self` if it's a reference, and `&mut self` if it's
3666+
a mutable reference. We should default to using `&self`, as it's the most
3667+
common.
3668+
3669+
Finally, as you may remember, the value of the area of a circle is `π*r²`.
3670+
Because we took the `&self` parameter to `area`, we can use it just like any
3671+
other parameter. Because we know it's a `Circle`, we can access the `radius`
3672+
just like we would with any other struct. An import of π and some
3673+
multiplications later, and we have our area.
3674+
3675+
You can also define methods that do not take a `self` parameter. Here's a
3676+
pattern that's very common in Rust code:
3677+
3678+
```
3679+
struct Circle {
3680+
x: f64,
3681+
y: f64,
3682+
radius: f64,
3683+
}
3684+
3685+
impl Circle {
3686+
fn new(x: f64, y: f64, radius: f64) -> Circle {
3687+
Circle {
3688+
x: x,
3689+
y: y,
3690+
radius: radius,
3691+
}
3692+
}
3693+
}
3694+
3695+
fn main() {
3696+
let c = Circle::new(0.0, 0.0, 2.0);
3697+
}
3698+
```
3699+
3700+
This **static method** builds a new `Circle` for us. Note that static methods
3701+
are called with the `Struct::method()` syntax, rather than the `ref.method()`
3702+
syntax.
3703+
3704+
36173705
# Closures
36183706

36193707
So far, we've made lots of functions in Rust. But we've given them all names.

0 commit comments

Comments
 (0)