From 435da6866f3483065d19d7da36403da92ba44325 Mon Sep 17 00:00:00 2001 From: Tshepang Lekhonkhobe Date: Thu, 26 Mar 2015 21:53:17 +0200 Subject: [PATCH] book: 'x' is already taken, so use something else --- src/doc/trpl/method-syntax.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/doc/trpl/method-syntax.md b/src/doc/trpl/method-syntax.md index c8309a1e4400c..d720f26744449 100644 --- a/src/doc/trpl/method-syntax.md +++ b/src/doc/trpl/method-syntax.md @@ -4,7 +4,7 @@ Functions are great, but if you want to call a bunch of them on some data, it can be awkward. Consider this code: ```{rust,ignore} -baz(bar(foo(x))); +baz(bar(foo))); ``` We would read this left-to right, and so we see "baz bar foo." But this isn't the @@ -12,7 +12,7 @@ order that the functions would get called in, that's inside-out: "foo bar baz." Wouldn't it be nice if we could do this instead? ```{rust,ignore} -x.foo().bar().baz(); +foo.bar().baz(); ``` Luckily, as you may have guessed with the leading question, you can! Rust provides @@ -47,11 +47,11 @@ This will print `12.566371`. We've made a struct that represents a circle. We then write an `impl` block, and inside it, define a method, `area`. 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 being the `x` in `x.foo()`. The three -variants correspond to the three kinds of thing `x` could be: `self` if it's +You can think of this first parameter as being the `foo` in `foo.bar()`. The three +variants correspond to the three kinds of things `foo` could be: `self` if it's just a value on the stack, `&self` if it's a reference, and `&mut self` if it's a mutable reference. We should default to using `&self`, as it's the most -common, as Rustaceans prefer borrowing over taking ownership, and references +common, as Rustaceans prefer borrowing over taking ownership, and references over mutable references. Here's an example of all three variants: ```rust