Recap - Printable Point
Part of the Object Oriented Programming section of Coddy's Rust journey — lesson 39 of 61.
Challenge
EasyLet'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 publicPointstruct with publicxandyfields (bothi32). Your Point should support three capabilities:- Debug printing with
{:?}— derive this automatically - User-friendly display with
{}in the format(x, y)— implement this manually usingstd::fmt::Display - Equality comparison with
==— derivePartialEq
- Debug printing with
main.rs: Bring in your point module and create twoPointinstances 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: trueAnd with inputs 10, 20, 5, 15:
Debug: Point { x: 10, y: 20 }
Display: (5, 15)
Equal: falseYou 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
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