Menu
Coddy logo textTech

Recap - Coordinate Point

Part of the Object Oriented Programming section of Coddy's Rust journey — lesson 28 of 61.

challenge icon

Challenge

Easy

Let's build a flexible coordinate system that works with any numeric type! You'll create a generic Point struct that can represent positions using integers for grid-based games or floating-point numbers for precise graphics—all from a single definition.

You'll organize your code across two files:

  • point.rs: Define a public generic Point<T> struct with two public fields: x and y, both of type T. Implement methods for your point:
    • A new associated function that creates a point from x and y coordinates
    • A translate method that takes dx and dy values and returns a new Point with the coordinates shifted by those amounts (this requires T to support addition and copying, so use the bound T: std::ops::Add<Output = T> + Copy)
  • main.rs: Bring in your point module and demonstrate how the same generic struct works seamlessly with different numeric types.

In your main file, showcase your generic Point by:

  1. Creating an integer point using the first two inputs (parsed as i32)
  2. Translating it by the third and fourth inputs (also i32)
  3. Creating a floating-point point using the fifth and sixth inputs (parsed as f64)
  4. Displaying all three points

Your output should follow this format:

Integer point: ({x}, {y})
After translation: ({x}, {y})
Float point: ({x}, {y})

For example, with inputs 3, 5, 2, -1, 1.5, and 2.5:

Integer point: (3, 5)
After translation: (5, 4)
Float point: (1.5, 2.5)

Notice how Point::new(3, 5) creates a Point<i32> while Point::new(1.5, 2.5) creates a Point<f64>—the same struct definition adapts to both!

You will receive six inputs: two integers for the first point, two integers for translation, and two floats for the second point.

Try it yourself

mod point;

use point::Point;

fn main() {
    // Read inputs
    let mut input = String::new();
    std::io::stdin().read_line(&mut input).expect("Failed to read line");
    let x1: i32 = input.trim().parse().expect("Invalid input");
    
    input.clear();
    std::io::stdin().read_line(&mut input).expect("Failed to read line");
    let y1: i32 = input.trim().parse().expect("Invalid input");
    
    input.clear();
    std::io::stdin().read_line(&mut input).expect("Failed to read line");
    let dx: i32 = input.trim().parse().expect("Invalid input");
    
    input.clear();
    std::io::stdin().read_line(&mut input).expect("Failed to read line");
    let dy: i32 = input.trim().parse().expect("Invalid input");
    
    input.clear();
    std::io::stdin().read_line(&mut input).expect("Failed to read line");
    let x2: f64 = input.trim().parse().expect("Invalid input");
    
    input.clear();
    std::io::stdin().read_line(&mut input).expect("Failed to read line");
    let y2: f64 = input.trim().parse().expect("Invalid input");
    
    // TODO: Create an integer point using x1 and y1
    
    // TODO: Translate the integer point by dx and dy
    
    // TODO: Create a floating-point point using x2 and y2
    
    // TODO: Print the results in the required format:
    // println!("Integer point: ({}, {})", ...);
    // println!("After translation: ({}, {})", ...);
    // println!("Float point: ({}, {})", ...);
}

All lessons in Object Oriented Programming