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

가변성

가변 데이터는 &mut T를 사용하여 가변적으로 빌려올 수 있습니다. 이를 _가변 참조(mutable reference)_라고 하며 빌리는 사람에게 읽기/쓰기 권한을 부여합니다. 반면 &T는 불변 참조를 통해 데이터를 빌려오며, 빌리는 사람은 데이터를 읽을 수는 있지만 수정할 수는 없습니다:

#[allow(dead_code)]
#[derive(Clone, Copy)]
struct Book {
    // `&'static str`은 읽기 전용 메모리에 할당된 문자열에 대한 참조입니다
    author: &'static str,
    title: &'static str,
    year: u32,
}

// 이 함수는 책에 대한 참조를 받습니다
fn borrow_book(book: &Book) {
    println!("{} - {} 판을 불변으로 빌렸습니다", book.title, book.year);
}

// 이 함수는 가변적인 책에 대한 참조를 받아 `year`를 2014로 변경합니다
fn new_edition(book: &mut Book) {
    book.year = 2014;
    println!("{} - {} 판을 가변으로 빌렸습니다", book.title, book.year);
}

fn main() {
    // `immutabook`이라는 이름의 불변 책을 생성합니다
    let immutabook = Book {
        // 문자열 리터럴은 `&'static str` 타입을 가집니다
        author: "더글라스 호프스태터",
        title: "괴델, 에셔, 바흐",
        year: 1979,
    };

    // `immutabook`의 가변 복사본을 생성하고 `mutabook`이라고 부릅니다
    let mut mutabook = immutabook;

    // 불변 객체를 불변으로 빌립니다
    borrow_book(&immutabook);

    // 가변 객체를 불변으로 빌립니다
    borrow_book(&mutabook);

    // 가변 객체를 가변으로 빌립니다
    new_edition(&mut mutabook);

    // 에러! 불변 객체는 가변으로 빌릴 수 없습니다
    new_edition(&mut immutabook);
    // FIXME ^ 이 줄을 주석 처리하세요
}

참고:

static