Rollup merge of #28225 - jackwilsonv:patch-3, r=steveklabnik

r? @steveklabnik
This commit is contained in:
Manish Goregaokar 2015-09-05 16:16:01 +05:30
commit 3610c731f3
1 changed files with 8 additions and 8 deletions

View File

@ -7,7 +7,7 @@ can be awkward. Consider this code:
baz(bar(foo)); baz(bar(foo));
``` ```
We would read this left-to right, and so we see baz bar foo. But this isnt the We would read this left-to-right, and so we see baz bar foo. But this isnt the
order that the functions would get called in, thats inside-out: foo bar baz. order that the functions would get called in, thats inside-out: foo bar baz.
Wouldnt it be nice if we could do this instead? Wouldnt it be nice if we could do this instead?
@ -45,17 +45,17 @@ This will print `12.566371`.
Weve made a struct that represents a circle. We then write an `impl` block, Weve made a `struct` that represents a circle. We then write an `impl` block,
and inside it, define a method, `area`. and inside it, define a method, `area`.
Methods take a special first parameter, of which there are three variants: Methods take a special first parameter, of which there are three variants:
`self`, `&self`, and `&mut self`. You can think of this first parameter as `self`, `&self`, and `&mut self`. You can think of this first parameter as
being the `foo` in `foo.bar()`. The three variants correspond to the three being the `foo` in `foo.bar()`. The three variants correspond to the three
kinds of things `foo` could be: `self` if its just a value on the stack, kinds of things `foo` could be: `self` if its just a value on the stack,
`&self` if its a reference, and `&mut self` if its a mutable reference. `&self` if its a reference, and `&mut self` if its a mutable reference.
Because we took the `&self` parameter to `area`, we can use it just like any Because we took the `&self` parameter to `area`, we can use it just like any
other parameter. Because we know its a `Circle`, we can access the `radius` other parameter. Because we know its a `Circle`, we can access the `radius`
just like we would with any other struct. just like we would with any other `struct`.
We should default to using `&self`, as you should prefer borrowing over taking We should default to using `&self`, as you should prefer borrowing over taking
ownership, as well as taking immutable references over mutable ones. Heres an ownership, as well as taking immutable references over mutable ones. Heres an
@ -120,12 +120,12 @@ Check the return type:
```rust ```rust
# struct Circle; # struct Circle;
# impl Circle { # impl Circle {
fn grow(&self) -> Circle { fn grow(&self, increment: f64) -> Circle {
# Circle } } # Circle } }
``` ```
We just say were returning a `Circle`. With this method, we can grow a new We just say were returning a `Circle`. With this method, we can grow a new
circle to any arbitrary size. `Circle` to any arbitrary size.
# Associated functions # Associated functions
@ -161,7 +161,7 @@ methods.
# Builder Pattern # Builder Pattern
Lets say that we want our users to be able to create Circles, but we will Lets say that we want our users to be able to create `Circle`s, but we will
allow them to only set the properties they care about. Otherwise, the `x` allow them to only set the properties they care about. Otherwise, the `x`
and `y` attributes will be `0.0`, and the `radius` will be `1.0`. Rust doesnt and `y` attributes will be `0.0`, and the `radius` will be `1.0`. Rust doesnt
have method overloading, named arguments, or variable arguments. We employ have method overloading, named arguments, or variable arguments. We employ
@ -224,7 +224,7 @@ fn main() {
} }
``` ```
What weve done here is make another struct, `CircleBuilder`. Weve defined our What weve done here is make another `struct`, `CircleBuilder`. Weve defined our
builder methods on it. Weve also defined our `area()` method on `Circle`. We builder methods on it. Weve also defined our `area()` method on `Circle`. We
also made one more method on `CircleBuilder`: `finalize()`. This method creates also made one more method on `CircleBuilder`: `finalize()`. This method creates
our final `Circle` from the builder. Now, weve used the type system to enforce our final `Circle` from the builder. Now, weve used the type system to enforce