From 441a896bdbfffb8fb9322165382e8ad54ce8d88c Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Thu, 4 Feb 2016 14:57:48 -0500 Subject: [PATCH 1/3] First draft: method syntax --- src/SUMMARY.md | 2 +- src/method-syntax.md | 266 +++++++++++++++++++++++++++++++++++++++++++ src/ownership.md | 9 +- 3 files changed, 274 insertions(+), 3 deletions(-) create mode 100644 src/method-syntax.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index a38dc47bb..97044d606 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -22,7 +22,7 @@ - [Slices](slices.md) - [Structs](structs.md) - - [Method Syntax]() + - [Method Syntax](method-syntax.md) - [Generics]() - [Advanced]() diff --git a/src/method-syntax.md b/src/method-syntax.md new file mode 100644 index 000000000..7b3188abf --- /dev/null +++ b/src/method-syntax.md @@ -0,0 +1,266 @@ +# Method Syntax + +In the last section on ownership, we made reference to several ‘methods’. +Methods look like this: + +```rust +let s1 = String::from("hello"); +let s2 = s1.clone(); + +println!("{}", s1); +``` + +The call to `clone()` is attatched to `s1` with a dot. This is called ‘method +syntax’, and it’s a way to call certain functions with a different style. + +Why have two ways to call functions? One big reason is that methods look nicer +when chained together: + +```rust,ignore +// with functions +h(g(f(x))); + +// with methods +x.f().g().h(); +``` + +The nested-functions version reads in reverse: we call `f()`, then `g()`, then +`h()`, but it reads as `h()`, then `g()`, then `f()`. + +Before we get into the details, let’s talk about how to define your own +methods. + +## Defining methods + +We can define methods with the `impl` keyword. `impl` is short for +‘implementation’. Doing so looks like this: + +```rust +#[derive(Debug,Copy,Clone)] +struct Point { + x: f64, + y: f64, +} + +impl Point { + fn distance(&self, other: &Point) -> f64 { + let x_squared = f64::powi(other.x - self.x, 2); + let y_squared = f64::powi(other.y - self.y, 2); + + f64::sqrt(x_squared + y_squared) + } +} + +let p1 = Point { x: 0.0, y: 0.0 }; +let p2 = Point { x: 5.0, y: 6.5 }; + +assert_eq!(8.200609733428363, p1.distance(&p2)); +``` + +Let’s break this down. First, we have our `Point` struct from earlier in the +chapter. Next comes our first use of the `impl` keyword: + +``` +# #[derive(Debug,Copy,Clone)] +# struct Point { +# x: f64, +# y: f64, +# } +# +impl Point { +# fn distance(&self, other: &Point) -> f64 { +# let x_squared = f64::powi(other.x - self.x, 2); +# let y_squared = f64::powi(other.y - self.y, 2); +# +# f64::sqrt(x_squared + y_squared) +# } +} +# +# let p1 = Point { x: 0.0, y: 0.0 }; +# let p2 = Point { x: 5.0, y: 6.5 }; +# +# assert_eq!(8.200609733428363, p1.distance(&p2)); +``` + +Everything we put inside of the curly braces will be methods implemented on +`Point`. + +``` +# #[derive(Debug,Copy,Clone)] +# struct Point { +# x: f64, +# y: f64, +# } +# +# impl Point { + fn distance(&self, other: &Point) -> f64 { +# let x_squared = f64::powi(other.x - self.x, 2); +# let y_squared = f64::powi(other.y - self.y, 2); +# +# f64::sqrt(x_squared + y_squared) + } +# } +# +# let p1 = Point { x: 0.0, y: 0.0 }; +# let p2 = Point { x: 5.0, y: 6.5 }; +# +# assert_eq!(8.200609733428363, p1.distance(&p2)); +``` + +Next is our definition. This looks very similar to our previous definition of +`distance()` as a function: + +```rust +# #[derive(Debug,Copy,Clone)] +# struct Point { +# x: f64, +# y: f64, +# } +fn distance(p1: Point, p2: Point) -> f64 { +# let x_squared = f64::powi(p2.x - p1.x, 2); +# let y_squared = f64::powi(p2.y - p1.y, 2); +# +# f64::sqrt(x_squared + y_squared) +# } +``` + +Other than this, the rest of the example is familliar: an implementation of +`distance()`, and using the method to find an answer. + +There are two differences. The first is in the first argument. Instead of a name +and a type, we have written `&self`. This is what distinguishes a method from a +function: using `self` inside of an `impl` block. Because we already know that +we are implementing this method on `Point`, we don’t need to write the type of +`self` out. However, we have written `&self`, not only `self`. This is because +we want to take our argument by reference rather than by ownership. In other +words, these two forms are the same: + +```rust,ignore +fn foo(self: &Point) +fn foo(&self) +``` + +Just like any other parameter, you can take `self` in three forms. Here’s the +list, with the most common form first: + +```rust,ignore +fn foo(&self) // take self by reference +fn foo(&mut self) // take self by mutable reference +fn foo(self) // take self by ownership +``` + +In this case, we only need a reference. We don’t plan on taking ownership, and +we don’t need to mutate either point. Taking by reference is by far the most +common form of method, followed by a mutable reference, and then occasionally +by ownership. + +### Methods and automatic referencing + +We’ve left out an important detail. It’s in this line of the example: + +``` +# #[derive(Debug,Copy,Clone)] +# struct Point { +# x: f64, +# y: f64, +# } +# +# impl Point { +# fn distance(&self, other: &Point) -> f64 { +# let x_squared = f64::powi(other.x - self.x, 2); +# let y_squared = f64::powi(other.y - self.y, 2); +# +# f64::sqrt(x_squared + y_squared) +# } +# } +# +# let p1 = Point { x: 0.0, y: 0.0 }; +# let p2 = Point { x: 5.0, y: 6.5 }; +# +assert_eq!(8.200609733428363, p1.distance(&p2)); +``` + +When we defined `distance()`, we took both `self` and the other argument by +reference. Yet, we needed a `&` for `p2` but not `p1`. What gives? + +This feature is called ‘automatic referencing’, and calling methods is one +of the few places in Rust that has behavior like this. Here’s how it works: +when you call a method with `self.(`, Rust will automatically add in `&`s +or `&mut`s to match the signature. In other words, these two are the same: + +```rust +# #[derive(Debug,Copy,Clone)] +# struct Point { +# x: f64, +# y: f64, +# } +# +# impl Point { +# fn distance(&self, other: &Point) -> f64 { +# let x_squared = f64::powi(other.x - self.x, 2); +# let y_squared = f64::powi(other.y - self.y, 2); +# +# f64::sqrt(x_squared + y_squared) +# } +# } +# let p1 = Point { x: 0.0, y: 0.0 }; +# let p2 = Point { x: 5.0, y: 6.5 }; +p1.distance(&p2); +(&p1).distance(&p2); +``` + +The first one looks much, much cleaner. Here’s another example: + +```rust +let mut s = String::from("Hello,"); + +s.push_str(" world!"); + +// The above is the same as: +// (&mut s).push_str(" world!"); + +assert_eq!("Hello, world!", s); +``` + +Because [`push_str()`] has the following signature: + +```rust,ignore +fn push_str(&mut self, string: &str) { +``` + +[`push_str()`]: http://doc.rust-lang.org/collections/string/struct.String.html#method.push_str + +## Methods can be called like functions + +Furthermore, if we have a method, we can also call it like a function: + +```rust +# #[derive(Debug,Copy,Clone)] +# struct Point { +# x: f64, +# y: f64, +# } +# +# impl Point { +# fn distance(&self, other: &Point) -> f64 { +# let x_squared = f64::powi(other.x - self.x, 2); +# let y_squared = f64::powi(other.y - self.y, 2); +# +# f64::sqrt(x_squared + y_squared) +# } +# } +# let p1 = Point { x: 0.0, y: 0.0 }; +# let p2 = Point { x: 5.0, y: 6.5 }; +let d1 = p1.distance(&p2); +let d2 = Point::distance(&p1, &p2); + +assert_eq!(d1, d2); +``` + +Instead of using `self.(`, we use `Point` and the namespace operator to call it +like a function instead. Because functions do not do the automatic referencing, +we must pass in `&p1` explicitly. + +While methods can be called like functions, functions cannot be called like +methods. If the first argument isn’t named `self`, it cannot be called like a +method. diff --git a/src/ownership.md b/src/ownership.md index 26471c63b..4030daed3 100644 --- a/src/ownership.md +++ b/src/ownership.md @@ -257,8 +257,11 @@ inexpensive. ## Clone But what if we _do_ want to deeply copy the `String`’s data, and not just the -`String` itself? There’s a common method for that: `clone()`. Here’s an example -of `clone()` in action: +`String` itself? There’s a common method for that: `clone()`. We will discuss +methods in the next section on [`struct`]s, but they’re a common enough feature +in many programming languages that you have probably seen them before. + +Here’s an example of the `clone()` method in action: ```rust let s1 = String::from("hello"); @@ -267,6 +270,8 @@ let s2 = s1.clone(); println!("{}", s1); ``` +[`struct`]: structs.html + This will work just fine. Remember our diagram from before? In this case, it _is_ doing this: From cf1c7cb590aefc3ba6e27240ab32aa647e41e037 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Wed, 9 Mar 2016 10:40:13 -0500 Subject: [PATCH 2/3] more edits --- src/method-syntax.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/method-syntax.md b/src/method-syntax.md index 7b3188abf..5b40f3dad 100644 --- a/src/method-syntax.md +++ b/src/method-syntax.md @@ -5,6 +5,8 @@ Methods look like this: ```rust let s1 = String::from("hello"); + +// call a method on our String let s2 = s1.clone(); println!("{}", s1); @@ -13,7 +15,8 @@ println!("{}", s1); The call to `clone()` is attatched to `s1` with a dot. This is called ‘method syntax’, and it’s a way to call certain functions with a different style. -Why have two ways to call functions? One big reason is that methods look nicer +Why have two ways to call functions? We’ll talk about some deeper reasons +related to ownership in a moment, but one big reason is that methods look nicer when chained together: ```rust,ignore @@ -186,7 +189,7 @@ reference. Yet, we needed a `&` for `p2` but not `p1`. What gives? This feature is called ‘automatic referencing’, and calling methods is one of the few places in Rust that has behavior like this. Here’s how it works: when you call a method with `self.(`, Rust will automatically add in `&`s -or `&mut`s to match the signature. In other words, these two are the same: +or `&mut`s to match the signature. In other words, these three are the same: ```rust # #[derive(Debug,Copy,Clone)] @@ -207,6 +210,7 @@ or `&mut`s to match the signature. In other words, these two are the same: # let p2 = Point { x: 5.0, y: 6.5 }; p1.distance(&p2); (&p1).distance(&p2); +Point::distance(&p1, &p2); ``` The first one looks much, much cleaner. Here’s another example: @@ -230,6 +234,13 @@ fn push_str(&mut self, string: &str) { [`push_str()`]: http://doc.rust-lang.org/collections/string/struct.String.html#method.push_str +This automatic referencing behavior works because methods have a clear receiver +— the type of `self` — and in most cases it’s clear given the receiver and name +of a method whether the method is just reading (so needs `&self`), mutating (so +`&mut self`), or consuming (so `self`). The fact that Rust makes borrowing +implicit for method receivers is a big part of making ownership ergonomic in +practice. + ## Methods can be called like functions Furthermore, if we have a method, we can also call it like a function: From 080657796fd1bc5969a8ce8101bfb8c976801177 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Wed, 9 Mar 2016 10:51:11 -0500 Subject: [PATCH 3/3] slightly more clear language --- src/method-syntax.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/method-syntax.md b/src/method-syntax.md index 5b40f3dad..00242081f 100644 --- a/src/method-syntax.md +++ b/src/method-syntax.md @@ -1,6 +1,6 @@ # Method Syntax -In the last section on ownership, we made reference to several ‘methods’. +In the last section on ownership, we made several references to ‘methods’. Methods look like this: ```rust