<!DOCTYPE html>
    <html lang="ko">
    <head>
        <title>Rust 튜토리얼 - 자기주도프로젝트</title>

        <meta charset="UTF-8">
        <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
        <meta content="utf-8" http-equiv="encoding">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
        <meta name="keywords" content="Rust, Programming, Learning">
        <meta name="description" content="Rust tutorial website based on tour_of_rust by 최석원">
        <meta name="theme-color" content="#ff6801"/>
        <meta http-equiv="Cache-Control" content="max-age=3600">
        
        <link rel="stylesheet" href="tour.css">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/night-owl.min.css">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.1/css/all.min.css">
        
        <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
        <link rel="/manifest" href="./site.webmanifest">
        
        <script src="//unpkg.com/@highlightjs/cdn-assets@11.7.0/highlight.min.js"></script>

        <script src="./tour.js" defer></script>
        <!-- <script>hljs.highlightAll();</script> -->
        <script src="./highlight.badge.min.js"></script>
    </head>
    <body>
        <div class="tour">
            <div class="header">
                <span class="title"><a href="index.html">Rust 튜토리얼</a></span>
                <span class="nav">
                <span class="toc"><a href="TOC_ko.html">목차</a></span>
            </div>
            <div class="page">
            <h1>Scope</h1>
            <p><em>범위</em></p>
<p>범위는 프로그램 내에서 변수가 유효한 영역을 정의합니다.</p>
<pre><code class="rust">fn main() {
    let x = 10;

    {
        let y = 20;
        println!("Inside the inner scope, x: {}, y: {}", x, y);
    }

    println!("Outside the inner scope, x: {}", x);
    // println!("Outside the inner scope, y: {}", y); // This will cause a compile error
}</code></pre>
<p>우측 예제에서는 r1, r2, r3라는 세 개의 참조가 선언되어 있으며, 각각 두 개의 다른 범위에서 초기화됩니다.</p>
<p>첫 번째 범위에서 r1과 r2는 s에 대한 불변 참조로 초기화됩니다.</p>
<p>이 범위가 종료되면, r1과 r2에 대한 참조는 더 이상 유효하지 않습니다.</p>
<p>두 번째 범위에서는 r3가 s에 대한 변경 가능한 참조로 초기화됩니다.</p>
<p>이렇게 하면 첫 번째 범위에서의 불변 참조와 겹치지 않으므로 빌림 규칙에 위배되지 않습니다.</p>
<p>마지막으로 모든 범위가 종료된 후에는 각 참조가 각각 다른 범위에서 초기화되었기 때문에 r1, r2, r3를 함께 출력할 수 있습니다.</p>
            <div class="bottomnav">
                <span class="back"><a href="14_ko.html" rel="prev">❮ 이전</a></span>
                <span class="next"><a href="16_ko.html" rel="next">다음 ❯</a></span>
            </div>
            </div>
            <div class="code">
            <iframe id="rust-playground" width="100%" src="https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn+main%28%29+%7B%0D%0A++++let+mut+s+%3D+String%3A%3Afrom%28%22hello%22%29%3B%0D%0A%0D%0A++++let+r1%3B%0D%0A++++let+r2%3B%0D%0A++++let+r3%3B%0D%0A%0D%0A++++%7B%0D%0A++++++++r1+%3D+%26s%3B%0D%0A++++++++r2+%3D+%26s%3B%0D%0A++++++++println%21%28%22%EC%95%88%EC%AA%BD+inner+%EB%B2%94%EC%9C%84%2C+r1%3A+%7B%7D%2C+r2%3A+%7B%7D%22%2C+r1%2C+r2%29%3B%0D%0A++++++++%2F%2F+r1%EA%B3%BC+r2%EC%9D%98+%EB%B2%94%EC%9C%84%EB%A5%BC+%EC%97%AC%EA%B8%B0%EC%97%90%EC%84%9C+%EC%A2%85%EB%A3%8C%EC%8B%9C%ED%82%A4%EA%B8%B0+%EC%9C%84%ED%95%B4+%EC%A4%91%EA%B4%84%ED%98%B8%EB%A5%BC+%EC%B6%94%EA%B0%80%ED%95%A9%EB%8B%88%EB%8B%A4.%0D%0A++++%7D%0D%0A%0D%0A++++%7B%0D%0A++++++++r3+%3D+%26mut+s%3B%0D%0A++++++++println%21%28%22%EC%95%88%EC%AA%BD+%EB%8B%A4%EB%A5%B8+inner+%EB%B2%94%EC%9C%84%2C+r3%3A+%7B%7D%22%2C+r3%29%3B%0D%0A++++%7D%0D%0A%0D%0A++++%2F%2F+%EC%9D%B4%EC%A0%9C+r1%EA%B3%BC+r2%EB%8A%94+%EB%8D%94+%EC%9D%B4%EC%83%81+%EC%82%AC%EC%9A%A9%EB%90%98%EC%A7%80+%EC%95%8A%EC%9C%BC%EB%AF%80%EB%A1%9C+%EC%B6%9C%EB%A0%A5%EC%97%90%EC%84%9C+%EC%A0%9C%EC%99%B8%ED%95%A9%EB%8B%88%EB%8B%A4.%0D%0A++++println%21%28%22%EB%B0%94%EA%B9%A5+%EC%AA%BD+inner+%EB%B2%94%EC%9C%84%2C+r3%3A+%7B%7D%22%2C+r3%29%3B%0D%0A%7D%0D%0A" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals" title="Rust Playground" loading="lazy"></iframe>
            </div>
        </div>
        <script>
        var pres = document.querySelectorAll("pre>code");
        for (var i = 0; i < pres.length; i++) {
            hljs.highlightElement(pres[i]);
        }
        var options = {
            loadDelay: 0,
            copyIconClass: "far fa-clipboard",
            checkIconClass: "fa fa-check text-success",
            blogURL: "http://rust-study.ajousw.kr/"
        };
        window.highlightJsBadge(options);
        </script>

        <footer>
          <p><a target="_blank" rel="noopener" href="https://www.youtube.com/c/SoftwareToolTime">아주대학교 Software Tool Time</a> - Rust 튜토리얼 (Basic)</p>
        </footer>
    </body>
</html>