Select Git revision
22_ko.html 6.97 KiB
<!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=1">
<meta name="keywords" content="Rust, Programming, Learning">
<meta name="description" content="Rust tutorial website based on tour_of_rust">
<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>소유권</h1>
<p><strong>Ownership</strong></p>
<p>소유권 정의: Rust에서 각 값은 하나의 소유자를 가지며, 소유자가 범위를 벗어나면 값이 할당 해제됩니다.</p>
<p>소유권 이전 방법을 예제를 통해 설명합니다 (예: 값을 새 변수에 할당하거나 함수에 전달할 때).</p>
<p>값이 범위를 벗어날 때 자동으로 호출되는 drop 함수를 소개합니다.</p>
<p>소유권 정의:</p>
<p>Rust에서는 각 값에 대해 하나의 소유자가 존재하며, 소유자가 해당 값을 관리합니다.</p>
<p>소유자가 범위를 벗어나면 Rust는 자동으로 해당 값의 메모리를 해제합니다.</p>
<p>소유권 이전 예제:</p>
<p>예제 1: 값을 새 변수에 할당할 때</p>
<p>s1이 가리키는 값의 소유권이 s2로 이전되어 s1은 사용할 수 없습니다.</p>
<pre><code class="rust">let s1 = String::from("hello");
let s2 = s1;</code></pre>
<p>예제 2: 함수에 값을 전달할 때</p>
<p>s가 takes_ownership 함수에 전달되면서 소유권이 이전되고 함수 내에서 값의 메모리가 해제됩니다.</p>
<pre><code class="rust">fn takes_ownership(s: String) {
println!("{}", s);
}
fn main() {
let s = String::from("hello");
takes_ownership(s);
// 여기에서 s는 사용할 수 없습니다.
}</code></pre>
<p><code>drop</code> 함수 소개:</p>
<p>Rust에서는 값이 범위를 벗어날 때 자동으로 drop 함수가 호출됩니다.</p>
<p>drop 함수는 메모리를 안전하게 해제하는 역할을 합니다.</p>
<p>이를 통해 개발자가 직접 메모리 해제를 관리할 필요가 없습니다.</p>
<p>소유권, 소유권 이전 방법, 그리고 drop 함수를 통해 Rust는 메모리 관리를 단순화하고 안전하게 할 수 있습니다.</p>
<p>이로 인해 메모리 누수나 세그멘테이션 폴트와 같은 문제를 효과적으로 방지할 수 있습니다.</p>
<div class="bottomnav">
<span class="back"><a href="21_ko.html" rel="prev">❮ 이전</a></span>
<span class="next"><a href="23_ko.html" rel="next">다음 ❯</a></span>