소유권

Ownership

러스트의 값마다 소유자를 두고 이동·반납 시점을 추적합니다.

···
html
<div class="visual move" data-step="0"><div class="visual-head"><strong>OWNERSHIP MOVE</strong><i></i></div><div class="visual-scene"><div class="move-owner">a <small>old owner</small></div><div class="move-path"><i>String</i></div><div class="move-owner">b <small>new owner</small></div><div class="move-warning">a cannot use it</div></div><div class="visual-caption"></div><div class="visual-dots"><b></b><b></b><b></b></div></div>
css
.visual{width:min(95vw,760px);height:min(91vh,330px);padding:clamp(9px,2.5vmin,18px);display:flex;flex-direction:column;gap:clamp(6px,1.7vmin,11px);border:1px solid var(--line);border-radius:13px;background:var(--surface);font:500 clamp(12px,3.4vmin,16px)/1.25 var(--font-sans,-apple-system),sans-serif}
.visual-head{display:flex;align-items:center;justify-content:space-between;gap:8px;min-height:19px;color:var(--accent)}.visual-head strong{font-size:1em}.visual-head i{display:block;width:8px;height:8px;flex:none;border-radius:50%;background:var(--accent);animation:visual-pulse 1.3s infinite}@keyframes visual-pulse{50%{opacity:.25}}
.visual-scene{position:relative;flex:1;min-height:0;overflow:hidden}.visual-caption{min-height:2.4em;display:grid;place-items:center;text-align:center}.visual-dots{display:flex;justify-content:center;gap:4px}.visual-dots b{display:block;width:15px;height:3px;border-radius:3px;background:var(--line)}.visual-dots b.on{background:var(--accent)}
.visual-scene *{box-sizing:border-box}.visual-scene .mono{font-family:ui-monospace,monospace}.visual-scene .muted{color:var(--muted)}.visual-scene .lit{border-color:var(--accent)!important;background:color-mix(in srgb,var(--accent) 14%,var(--surface))!important;color:var(--fg)!important}
.move .visual-scene{display:flex;align-items:center;justify-content:center;gap:0}.move-owner{width:31%;height:60%;border:1px solid var(--line);border-radius:8px;background:var(--bg);display:flex;flex-direction:column;align-items:center;justify-content:flex-start;padding-top:6px;font:700 18px ui-monospace,monospace}.move-owner small{font:500 12px var(--font-sans,-apple-system),sans-serif;color:var(--muted)}.move-path{flex:1;height:3px;background:var(--line);position:relative;transform:translateY(26px)}.move-path i{display:grid;place-items:center;position:absolute;top:-11px;left:-70px;width:68px;height:28px;border-radius:6px;background:var(--accent);color:var(--surface);font:600 12px ui-monospace,monospace;transition:left .5s}.move[data-step="1"] .move-path i{left:calc(100% - 2px)}.move[data-step="2"] .move-path i{left:calc(100% - 2px)}.move[data-step="2"] .move-owner:first-child{opacity:.3}.move-warning{position:absolute;bottom:0;left:0;right:0;text-align:center;color:var(--accent-3);opacity:0;transition:opacity .3s}.move[data-step="2"] .move-warning{opacity:1}
js
const root=document.querySelector('.visual'),cap=root.querySelector('.visual-caption'),dots=[...root.querySelectorAll('.visual-dots b')],captions=["a owns the String","Token moves to b","Old name a is unavailable"];let step=0;function advance(){root.dataset.step=String(step);cap.textContent=captions[step];dots.forEach((d,i)=>d.classList.toggle('on',i===step));if(root.classList.contains('generic')){root.querySelector('.generic-input').textContent=step===3?'"hi"':'7';root.querySelector('.generic-output').textContent=step===3?'"hi" : string':'7 : number'}if(root.classList.contains('union'))root.querySelector('.set-value').textContent=step%2?'value: "seven"':'value: 7';step=(step+1)%captions.length}advance();let timer=setInterval(advance,1100);root.addEventListener('pointerdown',()=>{clearInterval(timer);advance()})

Rust의 소유권 규칙은 값에 소유자를 연결합니다. 소유권을 다른 변수로 이동하면 이전 변수는 그 값을 더는 사용할 수 없습니다. 소유자가 범위를 벗어나면 값은 정리됩니다.

데모는 값 토큰이 a에서 b로 이동한 뒤 a 사용을 막는 흐름을 보여줍니다. 단순 Copy 타입은 복사되므로 모든 대입이 이동인 것은 아닙니다.

언제 쓰나

메모리 해제와 자원 생명주기를 컴파일 시점에 이해하고 설계할 때 중요합니다.

페이지로 열기 ↗