From 723852ebb8e7bbcb31a3db24318aefca8d880d1b Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Tue, 2 Aug 2016 17:45:58 -0400 Subject: [PATCH] Remove a bunch of repeated hidden code This is all exactly the same as the first code block, so it is still being tested. It looks weird with the indenting when the code is hidden, and it's harder for me to read when editing the src (but I will leave hidden code in if it lets us test unique code examples) --- src/ch05-01-method-syntax.md | 76 +++++------------------------------- 1 file changed, 9 insertions(+), 67 deletions(-) diff --git a/src/ch05-01-method-syntax.md b/src/ch05-01-method-syntax.md index 4d97018b8..a406a2011 100644 --- a/src/ch05-01-method-syntax.md +++ b/src/ch05-01-method-syntax.md @@ -64,67 +64,27 @@ 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, -# } -# +```rust,ignore 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)); +```rust,ignore +fn distance(&self, other: &Point) -> f64 { + // ... +} ``` 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, -# } +```rust,ignore 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) + // ... } ``` @@ -167,25 +127,7 @@ from using the value of `self` after the transformation happens. 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 }; -# +```rust,ignore assert_eq!(8.200609733428363, p1.distance(&p2)); ```