?를 사용한 Option 언패킹

Beginner

This tutorial is from open-source community. Access the source code

소개

이 랩에서는 Rust 의 ? 연산자를 사용하여 중첩된 match 문 없이도 Option 값을 쉽게 언패킹하는 방법을 살펴봅니다. ? 연산자는 OptionSome인 경우 기본 값을 빠르게 반환하고, OptionNone인 경우 함수를 종료하고 None을 반환하는 데 사용될 수 있습니다. 이 연산자는 코드를 더 읽기 쉽고 간결하게 만들기 위해 함께 연결될 수 있습니다.

참고: 랩에서 파일 이름을 지정하지 않은 경우 원하는 파일 이름을 사용할 수 있습니다. 예를 들어, main.rs를 사용하고 rustc main.rs && ./main으로 컴파일하고 실행할 수 있습니다.

?를 사용하여 옵션 언패킹하기

match 문을 사용하여 Option을 언패킹할 수 있지만, ? 연산자를 사용하는 것이 더 쉬운 경우가 많습니다. xOption인 경우, x?를 평가하면 xSome인 경우 기본 값을 반환하고, 그렇지 않으면 실행 중인 모든 함수를 종료하고 None을 반환합니다.

fn next_birthday(current_age: Option<u8>) -> Option<String> {
    // If `current_age` is `None`, this returns `None`.
    // If `current_age` is `Some`, the inner `u8` gets assigned to `next_age`
    let next_age: u8 = current_age? + 1;
    Some(format!("Next year I will be {}", next_age))
}

코드를 훨씬 더 읽기 쉽게 만들기 위해 여러 개의 ?를 함께 연결할 수 있습니다.

struct Person {
    job: Option<Job>,
}

#[derive(Clone, Copy)]
struct Job {
    phone_number: Option<PhoneNumber>,
}

#[derive(Clone, Copy)]
struct PhoneNumber {
    area_code: Option<u8>,
    number: u32,
}

impl Person {

    // Gets the area code of the phone number of the person's job, if it exists.
    fn work_phone_area_code(&self) -> Option<u8> {
        // This would need many nested `match` statements without the `?` operator.
        // It would take a lot more code - try writing it yourself and see which
        // is easier.
        self.job?.phone_number?.area_code
    }
}

fn main() {
    let p = Person {
        job: Some(Job {
            phone_number: Some(PhoneNumber {
                area_code: Some(61),
                number: 439222222,
            }),
        }),
    };

    assert_eq!(p.work_phone_area_code(), Some(61));
}

요약

축하합니다! ?를 사용한 옵션 언패킹 랩을 완료했습니다. LabEx 에서 더 많은 랩을 연습하여 실력을 향상시킬 수 있습니다.