ジェネリックデータ型

ジェネリクスを使って、具体的なフィールドの型を抽象化することができます:

#[derive(Debug)]
struct Point<T> {
    x: T,
    y: T,
}

impl<T> Point<T> {
    fn coords(&self) -> (&T, &T) {
        (&self.x, &self.y)
    }

    // fn set_x(&mut self, x: T)
}

fn main() {
    let integer = Point { x: 5, y: 10 };
    let float = Point { x: 1.0, y: 4.0 };
    println!("{integer:?} and {float:?}");
    println!("coords: {:?}", integer.coords());
}
This slide should take about 15 minutes.
  • Q: なぜTは2回も impl<T> Point<T> {} において指定されたのでしょうか?冗長ではありませんか?

    • なぜなら、これはジェネリクスに対してのジェネリックな実装の箇所だからです。それらは独立してジェネリックです。
    • つまり、そのようなメソッドは任意のTに対して定義されるということです。
    • It is possible to write impl Point<u32> { .. }.
      • Pointはそれでもなおジェネリックであり、 Point<f64>を使うことができます。しかし、このブロックでのメソッドはPoint<u32>に対してのみ利用可能となります。
  • Try declaring a new variable let p = Point { x: 5, y: 10.0 };. Update the code to allow points that have elements of different types, by using two type variables, e.g., T and U.