Lifetime

수명

Lifetime relationships ensure a reference does not outlive the value it points to.

···
html
<div class="r2 lifetime" data-step="0"><div class="r2-head">REFERENCE LIFETIME<span class="r2-live"></span></div><div class="r2-stage"><div class="r2-time-head"><span>start</span><span>time →</span><span>end</span></div><div class="r2-time-bar owner">value lives</div><div class="r2-time-bar ref">reference</div><div class="r2-time-bar invalid">invalid extension</div></div><div class="r2-foot"><span class="r2-caption"></span><span class="r2-progress"></span></div></div>
css
.r2{width:min(95vw,730px);height:min(92vh,330px);padding:clamp(9px,2.5vmin,17px);display:flex;flex-direction:column;gap:7px;border:1px solid var(--line);border-radius:13px;background:var(--surface);color:var(--fg);font:500 clamp(12px,3.4vmin,16px)/1.25 var(--font-sans,sans-serif)}.r2 *{box-sizing:border-box}.r2-head{display:flex;justify-content:space-between;align-items:center;min-height:17px;color:var(--accent);font-size:12px;font-weight:700;letter-spacing:.03em}.r2-live{height:7px;width:7px;border-radius:50%;background:var(--accent);animation:r2beat 1.1s infinite}@keyframes r2beat{50%{opacity:.25}}.r2-stage{flex:1;min-height:0;position:relative;overflow:hidden}.r2-foot{min-height:27px;display:flex;align-items:center;justify-content:space-between;gap:8px;font-size:12px}.r2-caption{min-width:0}.r2-progress{flex:none;color:var(--accent);font:600 12px ui-monospace,monospace}.lifetime .r2-stage{display:flex;flex-direction:column;justify-content:center;gap:5px}.r2-time-head{display:flex;justify-content:space-between;color:var(--muted);font-size:12px}.r2-time-bar{height:24px;padding:3px 8px;border-radius:5px;font-size:12px;white-space:nowrap;transition:width .35s,opacity .35s}.r2-time-bar.owner{width:72%;border:1px solid var(--accent);background:color-mix(in srgb,var(--accent) 18%,var(--surface))}.r2-time-bar.ref{width:56%;margin-left:12%;border:1px solid var(--accent-2);background:color-mix(in srgb,var(--accent-2) 18%,var(--surface))}.r2-time-bar.invalid{width:45%;margin-left:55%;border:1px dashed var(--accent-3);color:var(--accent-3);opacity:.2}.lifetime[data-step="1"] .r2-time-bar.ref{width:80%}.lifetime[data-step="1"] .r2-time-bar.invalid{opacity:1}.lifetime[data-step="2"] .r2-time-bar.ref{width:56%}.lifetime[data-step="2"] .r2-time-bar.invalid{opacity:.15}
js
const root=document.querySelector('.r2'),caption=root.querySelector('.r2-caption'),progress=root.querySelector('.r2-progress'),labels=["Value owns the valid range","Reference extends too far","Shorten the reference range"];let step=0;function advance(){root.dataset.step=String(step);caption.textContent=labels[step];progress.textContent=(step+1)+' / '+labels.length;step=(step+1)%labels.length}advance();const timer=setInterval(advance,1000);root.addEventListener('pointerdown',()=>{clearInterval(timer);advance()})

Rust lifetimes describe relationships between the valid ranges of references. The compiler rejects a reference that could remain usable after its referent disappears. Writing a lifetime annotation does not extend a value’s life.

In the demo, the value’s bar must cover the reference bar. Extending the reference beyond that boundary reveals an invalid segment. Explicit lifetime parameters clarify which input reference a returned reference is tied to.

When to use

Use lifetimes when a function returns a reference or a struct connects multiple references.

Open as page ↗