Generic Data Types

You can use generics to abstract over the concrete field type:

1. ```
   #[derive(Debug)]
   ```
2. ```
   struct Point<T> {
   ```
3. ```
       x: T,
   ```
4. ```
       y: T,
   ```
5. ```
   }
   ```

7. ```
   impl<T> Point<T> {
   ```
8. ```
       fn coords(&self) -> (&T, &T) {
   ```
9. ```
           (&self.x, &self.y)
   ```
10. ```
        }
    ```

12. ```
        fn set_x(&mut self, x: T) {
    ```
13. ```
            self.x = x;
    ```
14. ```
        }
    ```
15. ```
    }
    ```

17. ```
    fn main() {
    ```
18. ```
        let integer = Point { x: 5, y: 10 };
    ```
19. ```
        let float = Point { x: 1.0, y: 4.0 };
    ```
20. ```
        println!("{integer:?} and {float:?}");
    ```
21. ```
        println!("coords: {:?}", integer.coords());
    ```
22. ```
    }
    ```

This slide should take about 10 minutes.

  • Q: Why T is specified twice in impl<T> Point<T> {}? Isn’t that redundant?

  • This is because it is a generic implementation section for generic type. They are independently generic.

  • It means these methods are defined for any T.
  • It is possible to write impl Point<u32> { .. }.
    • Point is still generic and you can use Point<f64>, but methods in this block will only be available for 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.