1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/// # trait: 关联类型
///
/// Base usage:
///
/// ```
/// pub fn associated_type(){
///     #[derive(Debug, PartialEq)]
///     struct Foo(i32);
///     #[derive(Debug, PartialEq)]
///     struct Bar(i32, i32);
///     trait Inst {
///         type F;
///         type B;
///         fn new_foo(i: i32) -> Self::F;
///         fn new_bar(i: i32) -> Self::B;
///     }
///     struct FooBar;
///     impl Inst for FooBar {
///         type F = Foo;
///         type B = Bar;
///         fn new_foo(i: i32) -> Foo {
///             Foo(i)
///         }
///         fn new_bar(i: i32) -> Bar {
///             Bar(i, i + 10)
///         }
///     }
///     let f: Foo = FooBar::new_foo(10);
///     assert_eq!(f, Foo(10));
///     let b: Bar = FooBar::new_bar(20);
///     assert_eq!(b, Bar(20, 30));
/// }
/// infer_generics();
/// ```
pub fn associated_type(){
    #[derive(Debug, PartialEq)]
    struct Foo(i32);
    #[derive(Debug, PartialEq)]
    struct Bar(i32, i32);
    trait Inst {
        type F;
        type B;
        fn new_foo(i: i32) -> Self::F;
        fn new_bar(i: i32) -> Self::B;
    }
    struct FooBar;
    impl Inst for FooBar {
        type F = Foo;
        type B = Bar;
        fn new_foo(i: i32) -> Foo {
            Foo(i)
        }
        fn new_bar(i: i32) -> Bar {
            Bar(i, i + 10)
        }
    }
    let f: Foo = FooBar::new_foo(10);
    assert_eq!(f, Foo(10));
    let b: Bar = FooBar::new_bar(20);
    assert_eq!(b, Bar(20, 30));
}


/// # trait: 泛型trait
///
/// Base usage:
///
/// ```
/// fn generics_trait(){
///     trait Add<RHS, Output> {
///         fn my_add(self, rhs: RHS) -> Output;
///     }
///     impl Add<i32, i32> for  i32 {
///         fn my_add(self, rhs: i32) -> i32 {
///             self + rhs
///         }
///     }
///     impl Add<u32, i32> for  u32 {
///         fn my_add(self, rhs: u32) -> i32 {
///             (self + rhs ) as i32
///         }
///     }
///     let (a, b, c, d) = (1i32, 2i32, 3u32, 4u32);
///     let x: i32 = a.my_add(b);
///     let y: i32 = c.my_add(d);
///     assert_eq!(x, 3i32);
///     assert_eq!(y, 7i32);
/// }
/// generics_trait();
/// ```
pub fn generics_trait(){
    trait Add<RHS, Output> {
        fn my_add(self, rhs: RHS) -> Output;
    }
    impl Add<i32, i32> for  i32 {
        fn my_add(self, rhs: i32) -> i32 {
            self + rhs
        }
    }
    impl Add<u32, i32> for  u32 {
        fn my_add(self, rhs: u32) -> i32 {
            (self + rhs ) as i32
        }
    }
    let (a, b, c, d) = (1i32, 2i32, 3u32, 4u32);
    let x: i32 = a.my_add(b);
    let y: i32 = c.my_add(d);
    assert_eq!(x, 3i32);
    assert_eq!(y, 7i32);
}

/// # trait: 关联类型String用法
///
/// 说明:
/// 在Rust标准库中,`String`类型实现`Add trait`,使用了关联类型。
/// 如下所示:
/// ```
/// impl Add<&str> for String {
///     type Output = String;
///     fn add(mut self, other: &str) -> String {
///         self.push_str(other);
///         self
///     }
/// }
/// ```
/// Base usage:
///
/// ```
/// let a = "hello";
/// let b = " world";
/// let c = a.to_string() + b;
/// println!("{:?}", c); // "hello world"
/// ```
pub fn string_add(){
    let a = "hello";
    let b = " world";
    let c = a.to_string() + b;
    println!("{:?}", c); // "hello world"
}


/// # trait 一致性
///
/// Base usage: 错误的方式,违反孤儿规则
///
/// ```
/// use std::ops::Add;
/// impl Add<u64> for u32{
///     type Output = u64;
///     fn add(self, other: u64) -> Self::Output {
///         (self as u64) + other
///     }
/// }
/// let a = 1u32;
/// let b = 2u64;
/// assert_eq!(a+b, 3);
/// ```
///
/// Base usage: 正确的方式之重新定义trait
///
/// ```
/// trait Add<RHS=Self> {
/// type Output;
///     fn add(self, rhs: RHS) -> Self::Output;
/// }
/// impl Add<u64> for u32{
///     type Output = u64;
///     fn add(self, other: u64) -> Self::Output {
///         (self as u64) + other
///     }
/// }
/// let a = 1u32;
/// let b = 2u64;
/// assert_eq!(a.add(b), 3);
/// ```
///
/// Base usage: 正确的方式之重新定义类型
///
/// ```
/// use std::ops::Add;
/// #[derive(Debug)]
/// struct Point {
///     x: i32,
///     y: i32,
/// }
/// impl Add for Point {
///     type Output = Point;
///     fn add(self, other: Point) -> Point {
///         Point {
///             x: self.x + other.x,
///             y: self.y + other.y,
///         }
///    }
/// }
///
/// // Point { x: 3, y: 3 }
/// println!("{:?}", Point { x: 1, y: 0 } + Point { x: 2, y: 3 });
/// ```
pub fn override_op(){
    trait Add<RHS=Self> {
    type Output;
        fn add(self, rhs: RHS) -> Self::Output;
    }
    impl Add<u64> for u32{
        type Output = u64;
        fn add(self, other: u64) -> Self::Output {
            (self as u64) + other
        }
    }
    let a = 1u32;
    let b = 2u64;
    assert_eq!(a.add(b), 3);
}

/// # trait继承
///
/// Base usage:
///
/// ```
/// trait Page{
///     fn set_page(&self, p: i32){
///         println!("Page Default: 1");
///     }
/// }
/// trait PerPage{
///     fn set_perpage(&self, num: i32){
///         println!("Per Page Default: 10");
///     }
/// }
/// trait Paginate: Page + PerPage{
///     fn set_skip_page(&self, num: i32){
///         println!("Skip Page : {:?}", num);
///     }
/// }
/// impl <T: Page + PerPage>Paginate for T{}
/// struct MyPaginate{ page: i32 }
/// impl Page for MyPaginate{}
/// impl PerPage for MyPaginate{}
/// let my_paginate = MyPaginate{page: 1};
/// my_paginate.set_page(2);
/// my_paginate.set_perpage(100);
/// my_paginate.set_skip_page(12);
/// ```
pub fn trait_inherit(){
    trait Page{
        fn set_page(&self, p: i32){
            println!("Page Default: 1");
        }
    }
    trait PerPage{
        fn set_perpage(&self, num: i32){
            println!("Per Page Default: 10");
        }
    }
    trait Paginate: Page + PerPage{
        fn set_skip_page(&self, num: i32){
            println!("Skip Page : {:?}", num);
        }
    }
    impl <T: Page + PerPage>Paginate for T{}

    struct MyPaginate{ page: i32 }
    impl Page for MyPaginate{}
    impl PerPage for MyPaginate{}
    let my_paginate = MyPaginate{page: 1};
    my_paginate.set_page(2);
    my_paginate.set_perpage(100);
    my_paginate.set_skip_page(12);
}

/// # trait bound 泛型约束
///
/// Base usage:
///
/// ```
/// use std::ops::Add;
/// fn sum<T: Add<T, Output=T>>(a: T, b: T) -> T{
///     a + b
/// }
///
/// assert_eq!(sum(1u32, 2u32), 3);
/// assert_eq!(sum(1u64, 2u64), 3);
/// ```
pub fn trait_bound(){
    use std::ops::Add;
    fn sum<T: Add<T, Output=T>>(a: T, b: T) -> T{
        a + b
    }
    assert_eq!(sum(1u32, 2u32), 3);
    assert_eq!(sum(1u64, 2u64), 3);
}