constructing.rs 877 B

12345678910111213141516171819202122232425
  1. use point::Point;
  2. fn main() {
  3. // Default is used to obtain a Point to (0, 0)
  4. let p: Point<i32> = Point::default();
  5. // ^
  6. // Type is assigned here
  7. println!("p = {}", p); // (0, 0)
  8. let _p: Point<i32> = Point::<i32>::default();
  9. // ^
  10. // Type can be assigned here too
  11. // Giving just 1 number will assign both x and y
  12. let p1: Point<u16> = Point::from(3);
  13. println!("p1 = {}", p1); // (3, 3)
  14. // Giving a tuple of 2 numbers will assign x and y respectively
  15. let p2: Point<i64> = Point::from((9_i64, 81));
  16. // ^ ^
  17. // x y
  18. // x also adds the type hint that 9 is an i64 (Which defines the Point to i64)
  19. // y MUST be i64 also, no mix matching types
  20. println!("p2 = {}", p2);
  21. }