Menu
Coddy logo textTech

요약 - 사각형 동작

Coddy Rust 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨. 61개 중 7번째.

challenge icon

챌린지

쉬움

두 필드 width(u32)와 height(u32)를 가진 Rectangle struct를 Create하세요.

Rectangle에 다음을 포함하는 implementation block을 Add하세요.

  • new: width: u32height: u32takes하고 새로운 Rectanglereturns하는 associated function
  • area: &selftakes하고 넓이를 u32returns
  • scale: &mut selffactor: u32takes하고, width와 height 모두에 factor를 곱함

세 개의 입력을 받습니다.

  • 첫 번째 줄: width (u32)
  • 두 번째 줄: height (u32)
  • 세 번째 줄: scale factor (u32)

제공된 dimensions를 사용하여 Rectangle::new로 mutable Rectangle을 Create하세요. initial area를 Print한 다음, 주어진 factor로 rectangle을 Scale하고 new area를 Print하세요.

Expected output format:

{initial_area}
{scaled_area}

직접 해보기

use std::io;

// TODO: 여기에 Rectangle 구조체를 정의하세요


// TODO: 다음을 포함하는 Rectangle의 구현 블록을 추가하세요:
// - new: width와 height를 받아 Rectangle을 반환하는 연관 함수
// - area: &self를 받아 u32를 반환하는 메서드
// - scale: &mut self와 factor를 받아 width와 height를 곱하는 메서드


fn main() {
    let mut input = String::new();
    
    // 너비 읽기
    io::stdin().read_line(&mut input).expect("Failed to read line");
    let width: u32 = input.trim().parse().expect("Invalid number");
    
    input.clear();
    
    // 높이 읽기
    io::stdin().read_line(&mut input).expect("Failed to read line");
    let height: u32 = input.trim().parse().expect("Invalid number");
    
    input.clear();
    
    // 배율 인자 읽기
    io::stdin().read_line(&mut input).expect("Failed to read line");
    let factor: u32 = input.trim().parse().expect("Invalid number");
    
    // TODO: Rectangle::new를 사용하여 가변 Rectangle 생성
    
    // TODO: 초기 넓이 출력
    
    // TODO: factor로 사각형을 스케일
    
    // TODO: 새 넓이 출력
}

객체 지향 프로그래밍의 모든 레슨

직접 연습해 보세요: 온라인 Rust 컴파일러