Menu
Coddy logo textTech

Recap - Printable Point

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

challenge icon

Challenge

Easy

Let's bring together all the standard traits you've learned by creating a fully-featured Point struct that can be debugged, displayed beautifully, and compared for equality!

You'll organize your code across two files:

  • point.rs: Define a public Point struct with public x and y fields (both i32). Your Point should support three capabilities:
    • Debug printing with {:?} — derive this automatically
    • User-friendly display with {} in the format (x, y) — implement this manually using std::fmt::Display
    • Equality comparison with == — derive PartialEq
  • main.rs: Bring in your point module and create two Point instances using the inputs provided. Demonstrate all three capabilities by printing the first point with debug format, printing the second point with display format, and comparing whether the two points are equal.

Your output should show all three trait capabilities in action:

Debug: Point { x: {x1}, y: {y1} }
Display: ({x2}, {y2})
Equal: {true/false}

For example, with inputs 3, 5, 3, 5:

Debug: Point { x: 3, y: 5 }
Display: (3, 5)
Equal: true

And with inputs 10, 20, 5, 15:

Debug: Point { x: 10, y: 20 }
Display: (5, 15)
Equal: false

You will receive four inputs: the x and y coordinates for the first point, followed by the x and y coordinates for the second point. Parse each as i32.

Try it yourself

mod point;

use point::Point;

fn main() {
    // Read four inputs: x1, y1, x2, y2
    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 x2: i32 = input.trim().parse().expect("Invalid input");

    input.clear();
    std::io::stdin().read_line(&mut input).expect("Failed to read line");
    let y2: i32 = input.trim().parse().expect("Invalid input");

    // TODO: Create two Point instances using the inputs

    // TODO: Print the first point using debug format {:?}
    // Format: "Debug: Point { x: ..., y: ... }"

    // TODO: Print the second point using display format {}
    // Format: "Display: (..., ...)"

    // TODO: Compare the two points and print the result
    // Format: "Equal: true" or "Equal: false"
}

All lessons in Object Oriented Programming