Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

생략

일부 라이프타임 패턴은 압도적으로 흔해서, 빌림 검사기는 타이핑을 줄이고 가독성을 높이기 위해 이를 생략할 수 있게 해줍니다. 이를 생략(elision)이라고 합니다. 생략은 오직 이러한 패턴이 흔하다는 이유만으로 Rust에 존재합니다.

다음 코드는 생략의 몇 가지 예시를 보여줍니다. 생략에 대한 더 포괄적인 설명은 책의 라이프타임 생략을 참조하세요.

// `elided_input`과 `annotated_input`은 본질적으로 동일한 시그니처를 가집니다.
// `elided_input`의 라이프타임이 컴파일러에 의해 추론되기 때문입니다:
fn elided_input(x: &i32) {
    println!("`elided_input`: {}", x);
}

fn annotated_input<'a>(x: &'a i32) {
    println!("`annotated_input`: {}", x);
}

// 마찬가지로, `elided_pass`와 `annotated_pass`는 동일한 시그니처를 가집니다.
// 라이프타임이 `elided_pass`에 암시적으로 추가되기 때문입니다:
fn elided_pass(x: &i32) -> &i32 { x }

fn annotated_pass<'a>(x: &'a i32) -> &'a i32 { x }

fn main() {
    let x = 3;

    elided_input(&x);
    annotated_input(&x);

    println!("`elided_pass`: {}", elided_pass(&x));
    println!("`annotated_pass`: {}", annotated_pass(&x));
}

참고:

생략