Skip to content

Commit 83d0b2c

Browse files
committed
---
yaml --- r: 223172 b: refs/heads/auto c: 556b081 h: refs/heads/master v: v3
1 parent 0bf5d23 commit 83d0b2c

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
88
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
99
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1010
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
11-
refs/heads/auto: 0e92165eafee26e0fb27f9a3756e8a68884d685d
11+
refs/heads/auto: 556b0815d779a50a151c44032febf3ce253e621e
1212
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1313
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336
1414
refs/tags/0.2: 1754d02027f2924bed83b0160ee340c7f41d5ea1

branches/auto/src/doc/trpl/operators-and-overloading.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,55 @@ will let you do this:
8181
let p: Point = // ...
8282
let x: f64 = p + 2i32;
8383
```
84+
85+
# Using operator traits in generic structs
86+
87+
Now that we know how operator traits are defined, we can define our `HasArea`
88+
trait and `Square` struct from the [traits chapter][traits] more generically:
89+
90+
[traits]: traits.html
91+
92+
```rust
93+
use std::ops::Mul;
94+
95+
trait HasArea<T> {
96+
fn area(&self) -> T;
97+
}
98+
99+
struct Square<T> {
100+
x: T,
101+
y: T,
102+
side: T,
103+
}
104+
105+
impl<T> HasArea<T> for Square<T>
106+
where T: Mul<Output=T> + Copy {
107+
fn area(&self) -> T {
108+
self.side * self.side
109+
}
110+
}
111+
112+
fn main() {
113+
let s = Square {
114+
x: 0.0f64,
115+
y: 0.0f64,
116+
side: 12.0f64,
117+
};
118+
119+
println!("Area of s: {}", s.area());
120+
}
121+
```
122+
123+
For `HasArea` and `Square`, we just declare a type parameter `T` and replace
124+
`f64` with it. The `impl` needs more involved modifications:
125+
126+
```ignore
127+
impl<T> HasArea<T> for Square<T>
128+
where T: Mul<Output=T> + Copy { ... }
129+
```
130+
131+
The `area` method requires that we can multiply the sides, so we declare that
132+
type `T` must implement `std::ops::Mul`. Like `Add`, mentioned above, `Mul`
133+
itself takes an `Output` parameter: since we know that numbers don't change
134+
type when multiplied, we also set it to `T`. `T` must also support copying, so
135+
Rust doesn't try to move `self.side` into the return value.

branches/auto/src/doc/trpl/traits.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,13 @@ equality.
199199

200200
[PartialEq]: ../core/cmp/trait.PartialEq.html
201201

202+
Here we defined a new struct `Rectangle` that accepts numbers of any
203+
precision—really, objects of pretty much any type—as long as they can be
204+
compared for equality. Could we do the same for our `HasArea` structs, `Square`
205+
and `Circle`? Yes, but they need multiplication, and to work with that we need
206+
to know more about [operator traits][operators-and-overloading].
202207

208+
[operators-and-overloading]: operators-and-overloading.html
203209

204210
# Rules for implementing traits
205211

0 commit comments

Comments
 (0)