File tree Expand file tree Collapse file tree 3 files changed +59
-1
lines changed
branches/auto/src/doc/trpl Expand file tree Collapse file tree 3 files changed +59
-1
lines changed Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
8
8
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
9
9
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
10
10
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
11
- refs/heads/auto: 0e92165eafee26e0fb27f9a3756e8a68884d685d
11
+ refs/heads/auto: 556b0815d779a50a151c44032febf3ce253e621e
12
12
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
13
13
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336
14
14
refs/tags/0.2: 1754d02027f2924bed83b0160ee340c7f41d5ea1
Original file line number Diff line number Diff line change @@ -81,3 +81,55 @@ will let you do this:
81
81
let p: Point = // ...
82
82
let x: f64 = p + 2i32;
83
83
```
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.
Original file line number Diff line number Diff line change @@ -199,7 +199,13 @@ equality.
199
199
200
200
[ PartialEq ] : ../core/cmp/trait.PartialEq.html
201
201
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 ] .
202
207
208
+ [ operators-and-overloading ] : operators-and-overloading.html
203
209
204
210
# Rules for implementing traits
205
211
You can’t perform that action at this time.
0 commit comments