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
/// # 泛型
///
/// Base usage: 自定义泛型函数
///
/// ```
/// fn foo<T>(x: T) -> T {
///    return x;
/// }
///
/// assert_eq!(foo(1), 1);
/// assert_eq!(foo("hello"), "hello");
/// ```
///
/// Base usage: 自定义泛型结构体
///
/// ```
/// struct Point<T> {  x: T, y: T }
/// ```
///
/// Base usage: `foo<T>`静态分发等价于下面代码
///
/// ```
/// fn foo_1(x: i32) -> i32 {
///     return x;
/// }
/// fn foo_2(x: &'static str) -> &'static str {
///     return x;
/// }
/// foo_1(1);
/// foo_2("2");
/// ```
pub fn foo<T>(x: T) -> T {
   return x;
}

/// # 泛型: 为泛型结构体实现方法
///
/// Base usage:
///
/// ```
/// fn impl_method(){
///     #[derive(Debug, PartialEq)]
///     struct Point<T> {x: T, y: T}
///     impl<T> Point<T> {
///         fn new(x: T, y: T) -> Self{
///             Point{x: x, y: y}
///         }
///     }
///     let point1 = Point::new(1, 2);
///     let point2 = Point::new("1", "2");
///     assert_eq!(point1, Point{x: 1, y: 2});
///     assert_eq!(point2, Point{x: "1", y: "2"});
/// }
/// impl_method();
/// ```
pub fn impl_method(){
    #[derive(Debug, PartialEq)]
    struct Point<T> {x: T, y: T}
    impl<T> Point<T> {
        fn new(x: T, y: T) -> Self{
            Point{x: x, y: y}
        }
    }
    let point1 = Point::new(1, 2);
    let point2 = Point::new("1", "2");
    assert_eq!(point1, Point{x: 1, y: 2});
    assert_eq!(point2, Point{x: "1", y: "2"});
}

/// # 泛型: 返回值自动推导
///
/// Base usage:
///
/// ```
/// fn  infer_generics(){
///     #[derive(Debug, PartialEq)]
///     struct Foo(i32);
///     #[derive(Debug, PartialEq)]
///     struct Bar(i32, i32);
///     trait Inst {
///         fn new(i: i32) -> Self;
///     }
///     impl Inst for Foo {
///         fn new(i: i32) -> Foo {
///             Foo(i)
///       }
///     }
///     impl Inst for Bar {
///        fn new(i: i32) -> Bar {
///            Bar(i, i + 10)
///        }
///     }
///     fn foobar<T: Inst>(i: i32) -> T {
///       T::new(i)
///     }
///     let f: Foo = foobar(10);
///     assert_eq!(f, Foo(10));
///     let b: Bar = foobar(20);
///     assert_eq!(b, Bar(20, 30));
/// }
/// infer_generics();
/// ```
pub fn infer_generics(){
    #[derive(Debug, PartialEq)]
    struct Foo(i32);
    #[derive(Debug, PartialEq)]
    struct Bar(i32, i32);
    trait Inst {
        fn new(i: i32) -> Self;
    }
    impl Inst for Foo {
        fn new(i: i32) -> Foo {
            Foo(i)
      }
    }
    impl Inst for Bar {
       fn new(i: i32) -> Bar {
           Bar(i, i + 10)
       }
    }
    fn foobar<T: Inst>(i: i32) -> T {
      T::new(i)
    }
    let f: Foo = foobar(10);
    assert_eq!(f, Foo(10));
    let b: Bar = foobar(20);
    assert_eq!(b, Bar(20, 30));

}