[][src]Function tao_of_rust::ch03::generics::impl_method

pub fn impl_method()

泛型: 为泛型结构体实现方法

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();Run