Recap - Coordinate Point
Part of the Object Oriented Programming section of Coddy's Rust journey — lesson 28 of 61.
Challenge
EasyLet'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 genericPoint<T>struct with two public fields:xandy, both of typeT. Implement methods for your point:- A
newassociated function that creates a point from x and y coordinates - A
translatemethod that takes dx and dy values and returns a newPointwith the coordinates shifted by those amounts (this requiresTto support addition and copying, so use the boundT: std::ops::Add<Output = T> + Copy)
- A
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:
- Creating an integer point using the first two inputs (parsed as
i32) - Translating it by the third and fourth inputs (also
i32) - Creating a floating-point point using the fifth and sixth inputs (parsed as
f64) - 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
1Methods and Behavior
Intro Implementation BlocksThe Self ParameterMutable MethodsAssociated FunctionsMultiple Implementation BlocksMethod ChainingRecap - Rectangle Actions4Project: Virtual Pet
Defining the PetFeeding the Pet7Standard Traits
The Debug TraitThe Display TraitClone and CopyEquality TraitsRecap - Printable Point10Project: Document System
The Draw TraitText Component2Encapsulation and Modules
Modules BasicsThe Public KeywordPrivate FieldsGettersSettersRecap - Secure Locker5Generics
Generic StructsGeneric MethodsMultiple Generic TypesGeneric FunctionsRecap - Coordinate Point8Traits as Bounds
Trait Bounds SyntaxMultiple BoundsThe Where ClauseReturning Types with TraitsRecap - Generic Printer11Design Patterns in Rust
Newtype PatternCompositionThe Drop TraitFrom and IntoRecap - Smart Pointer Mock