요약 - 출력 가능한 Point
Coddy Rust 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨. 61개 중 39번째.
챌린지
쉬움배운 모든 표준 트레잇을 하나로 모아, 디버깅하고 보기 좋게 표시하며 동등성을 비교할 수 있는 완전한 기능의 Point 구조체를 만들어 봅시다!
코드를 두 파일로 구성합니다:
point.rs: publicPoint구조체를 Define하고, publicx및yfields(i32both)를 정의합니다. Point는 다음 세 가지 기능을 지원해야 합니다:{:?}를 사용한 Debug 출력. 이를 automatically derive합니다{}를 사용한 사용자 친화적인 표시, 형식은(x, y):std::fmt::Display를 using하여 이를 manually Implement합니다==를 사용한 동등성 비교:PartialEq를 derive합니다
main.rs: point 모듈을 가져오고 제공된 inputs를 사용하여 두 개의Pointinstances를 만듭니다. 첫 번째 점을 Debug 형식으로 출력하고, 두 번째 점을 Display 형식으로 출력하며, 두 점이 Equal한지 비교하여 세 가지 기능을 모두 보여 줍니다.
출력에는 세 가지 트레잇 기능이 모두 실제로 사용된 결과가 나타나야 합니다:
Debug: Point { x: {x1}, y: {y1} }
Display: ({x2}, {y2})
Equal: {true/false}예를 들어 inputs가 3, 5, 3, 5인 경우:
Debug: Point { x: 3, y: 5 }
Display: (3, 5)
Equal: true그리고 inputs가 10, 20, 5, 15인 경우:
Debug: Point { x: 10, y: 20 }
Display: (5, 15)
Equal: false네 개의 inputs를 받습니다. 첫 번째 점의 x 및 y 좌표에 이어 두 번째 점의 x 및 y 좌표가 주어집니다. 각각을 i32로 parse합니다.
직접 해보기
mod point;
use point::Point;
fn main() {
// 네 개의 입력을 읽습니다: 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: 입력을 사용하여 두 개의 Point 인스턴스를 생성하세요
// TODO: 디버그 형식 {:?}을 사용하여 첫 번째 점을 출력하세요
// 형식: "Debug: Point { x: ..., y: ... }"
// TODO: 디스플레이 형식 {}을 사용하여 두 번째 점을 출력하세요
// 형식: "Display: (..., ...)"
// TODO: 두 점을 비교하고 결과를 출력하세요
// 형식: "Equal: true" 또는 "Equal: false"
}
객체 지향 프로그래밍의 모든 레슨
직접 연습해 보세요: 온라인 Rust 컴파일러