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

트레이트

트레이트 메서드의 라이프타임 어노테이션은 기본적으로 함수와 유사합니다. impl도 라이프타임 어노테이션을 가질 수 있음에 유의하세요.

// 라이프타임 어노테이션이 있는 구조체.
#[derive(Debug)]
struct Borrowed<'a> {
    x: &'a i32,
}

// impl에 라이프타임을 어노테이션합니다.
impl<'a> Default for Borrowed<'a> {
    fn default() -> Self {
        Self {
            x: &10,
        }
    }
}

fn main() {
    let b: Borrowed = Default::default();
    println!("b는 {:?}입니다", b);
}

참고:

trait