File tree Expand file tree Collapse file tree 3 files changed +59
-1
lines changed
branches/try/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 1
1
---
2
2
refs/heads/master: aca2057ed5fb7af3f8905b2bc01f72fa001c35c8
3
3
refs/heads/snap-stage3: 1af31d4974e33027a68126fa5a5a3c2c6491824f
4
- refs/heads/try: 0e92165eafee26e0fb27f9a3756e8a68884d685d
4
+ refs/heads/try: 556b0815d779a50a151c44032febf3ce253e621e
5
5
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
6
6
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
7
7
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
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