빌림

Borrowing

소유권을 옮기지 않고 참조를 빌려 읽거나 수정합니다.

···
html
<div class="visual borrow" data-step="0"><div class="visual-head"><strong>BORROW RULE</strong><i></i></div><div class="visual-scene"><div class="borrow-axis"><span>start</span><span>time →</span><span>end</span></div><div class="borrow-bar">read 1</div><div class="borrow-bar">read 2</div><div class="borrow-write">write waits</div></div><div class="visual-caption"></div><div class="visual-dots"><b></b><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}
.borrow .visual-scene{display:flex;flex-direction:column;justify-content:center;gap:4px}.borrow-axis{display:flex;justify-content:space-between;color:var(--muted);font-size:12px}.borrow-bar,.borrow-write{border-radius:5px;padding:2px 7px;height:20px;white-space:nowrap;font-size:12px;transition:width .35s,opacity .3s}.borrow-bar{width:60%;background:color-mix(in srgb,var(--accent-2) 24%,var(--surface));border:1px solid var(--accent-2)}.borrow-bar:nth-of-type(3){margin-left:20%;width:50%}.borrow-write{margin-left:72%;width:27%;border:1px solid var(--accent-3);background:color-mix(in srgb,var(--accent-3) 16%,var(--surface));opacity:.5}.borrow[data-step="0"] .borrow-bar{width:25%}.borrow[data-step="0"] .borrow-bar:nth-of-type(3){opacity:0}.borrow[data-step="1"] .borrow-bar:nth-of-type(3){width:20%}.borrow[data-step="2"] .borrow-write{opacity:1}.borrow[data-step="3"] .borrow-bar{opacity:.25}.borrow[data-step="3"] .borrow-write{margin-left:45%;width:50%;opacity:1}
js
const root=document.querySelector('.visual'),cap=root.querySelector('.visual-caption'),dots=[...root.querySelectorAll('.visual-dots b')],captions=["One shared reader","Two readers overlap","Writer cannot overlap","Writer starts after readers"];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에서는 &T 참조를 여러 개 빌려 읽을 수 있지만, 같은 시점에 수정 참조 &mut T와 겹치게 빌릴 수 없습니다. 이 규칙은 데이터 경쟁과 무효 참조를 막는 데 기여합니다.

데모는 읽기 참조 둘을 켠 다음 수정 참조를 켜려 할 때 충돌하는 상태를 보여줍니다. 참조가 마지막으로 사용되는 범위에 따라 실제 대여 구간이 달라집니다.

언제 쓰나

함수가 값을 소유하지 않고 읽거나 변경해야 할 때 참조를 전달합니다.

페이지로 열기 ↗