-
Rust연습 - 구조체 및 열거형을 사용하여 코드 수정(exercise-structs-enums)코딩Coding/Rust연습 2021. 5. 11. 17:49728x90
struct Car { color: String, transmission: Transmission, convertible: bool, mileage: u32, } #[derive(PartialEq, Debug)] enum Transmission { Manual, SemiAuto, Automatic, } fn car_factory(color: String, transmission: Transmission, convertible: bool) -> Car { // To Do - Fix this part of the function to create a new Car object as requested by the client let car = Car { color: color, transmission: transmission, convertible: convertible, mileage: 0, }; // Factory's Quality Control Department says that new cars must always have zero mileage! assert_eq!(car.mileage, 0); // Display the details of the new car order if car.convertible { println!( "New car = {}, {:?}, Convertible", car.color, car.transmission ); } else { println!("New car = {}, {:?}, Hardtop", car.color, car.transmission); } return car; } fn main() { let client_request_1 = car_factory(String::from("Red"), Transmission::Manual, false); assert_eq!(client_request_1.color, "Red"); assert_eq!(client_request_1.transmission, Transmission::Manual); assert_eq!(client_request_1.convertible, false); let client_request_2 = car_factory(String::from("Silver"), Transmission::Automatic, true); assert_eq!(client_request_2.color, "Silver"); assert_eq!(client_request_2.transmission, Transmission::Automatic); assert_eq!(client_request_2.convertible, true); let client_request_2 = car_factory(String::from("Yellow"), Transmission::SemiAuto, false); assert_eq!(client_request_2.color, "Yellow"); assert_eq!(client_request_2.transmission, Transmission::SemiAuto); assert_eq!(client_request_2.convertible, false); }
결과
docs.microsoft.com/ko-kr/learn/modules/rust-understand-common-concepts/5-exercise-structs-enums
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556struct Car {color: String,transmission: Transmission,convertible: bool,mileage: u32,}#[derive(PartialEq, Debug)]enum Transmission {Manual,SemiAuto,Automatic,}fn car_factory(color: String, transmission: Transmission, convertible: bool) -> Car {// To Do - Fix this part of the function to create a new Car object as requested by the clientlet car = Car {color: color,transmission: transmission,convertible: convertible,mileage: 0,};// Factory's Quality Control Department says that new cars must always have zero mileage!assert_eq!(car.mileage, 0);// Display the details of the new car orderif car.convertible {println!("New car = {}, {:?}, Convertible",car.color, car.transmission);} else {println!("New car = {}, {:?}, Hardtop", car.color, car.transmission);}return car;}fn main() {let client_request_1 = car_factory(String::from("Red"), Transmission::Manual, false);assert_eq!(client_request_1.color, "Red");assert_eq!(client_request_1.transmission, Transmission::Manual);assert_eq!(client_request_1.convertible, false);let client_request_2 = car_factory(String::from("Silver"), Transmission::Automatic, true);assert_eq!(client_request_2.color, "Silver");assert_eq!(client_request_2.transmission, Transmission::Automatic);assert_eq!(client_request_2.convertible, true);let client_request_2 = car_factory(String::from("Yellow"), Transmission::SemiAuto, false);assert_eq!(client_request_2.color, "Yellow");assert_eq!(client_request_2.transmission, Transmission::SemiAuto);assert_eq!(client_request_2.convertible, false);}cs 반응형'코딩Coding > Rust연습' 카테고리의 다른 글
Rust연습) Guessing Game in Rust (0) 2021.12.04 Rust연습_Fibonacci피보나치 출력 (0) 2021.05.13 Rust연습_피보나치 수열 출력하기 (0) 2021.05.13 Rust연습_Create a Calculator App러스트로 계산기 만들기-Engineer man (0) 2021.05.11 Rust연습_추리 게임 튜토리얼 (0) 2021.05.09 Rust연습_Booleans-bool 형식 (0) 2021.05.08 Rust연습_문자 및 문자열출력Character and strings (0) 2021.05.08 Rust연습_ The default type is f64. On modern CPUs, the f64 type is roughly the same speed as the f32 type, but it has more precision. (0) 2021.05.08