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

superself

superself 키워드는 아이템에 접근할 때 모호함을 제거하고 경로의 불필요한 하드코딩을 방지하기 위해 경로 내에서 사용될 수 있습니다.

fn function() {
    println!("`function()` 호출됨");
}

mod cool {
    pub fn function() {
        println!("`cool::function()` 호출됨");
    }
}

mod my {
    fn function() {
        println!("`my::function()` 호출됨");
    }

    mod cool {
        pub fn function() {
            println!("`my::cool::function()` 호출됨");
        }
    }

    pub fn indirect_call() {
        // 이 스코프에서 `function`이라는 이름의 모든 함수에 접근해 봅시다!
        print!("`my::indirect_call()` 호출됨, 결과는\n> ");

        // `self` 키워드는 현재 모듈 스코프를 가리킵니다. 이 경우 `my`입니다.
// `self::function()`을 호출하는 것과 `function()`을 직접 호출하는 것은
// 동일한 함수를 가리키기 때문에 같은 결과를 냅니다.
        self::function();
        function();

        // `self`를 사용하여 `my` 내부의 다른 모듈에 접근할 수도 있습니다:
        self::cool::function();

        // `super` 키워드는 부모 스코프(`my` 모듈 외부)를 가리킵니다.
        super::function();

        // 이는 *크레이트* 스코프에 있는 `cool::function`에 바인딩됩니다.
// 이 경우 크레이트 스코프는 가장 바깥쪽 스코프입니다.
        {
            use crate::cool::function as root_function;
            root_function();
        }
    }
}

fn main() {
    my::indirect_call();
}