Trait bound

트레이트 제약

A trait bound limits a generic function to types that provide a required behavior.

···
html
<div class="r2 trait" data-step="0"><div class="r2-head">TRAIT BOUND<span class="r2-live"></span></div><div class="r2-stage"><div class="r2-trait-demand">T: Display</div><div class="r2-trait-candidates"><span>String ✓</span><span>Photo ✕</span></div><div class="r2-trait-gate">format(value)</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}.trait .r2-stage{display:grid;align-content:center;justify-items:center;gap:6px}.r2-trait-demand,.r2-trait-gate{padding:5px 11px;border:1px solid var(--accent);border-radius:7px;background:var(--bg);font:600 12px ui-monospace,monospace}.r2-trait-candidates{display:flex;gap:7px}.r2-trait-candidates span{padding:6px;border:1px solid var(--line);border-radius:7px;background:var(--bg);font:600 12px ui-monospace,monospace;transition:opacity .3s,transform .3s,background .3s}.trait[data-step="0"] .r2-trait-demand,.trait[data-step="1"] .r2-trait-candidates span:first-child,.trait[data-step="2"] .r2-trait-gate{background:color-mix(in srgb,var(--accent) 15%,var(--surface));transform:translateY(-3px)}.trait[data-step="1"] .r2-trait-candidates span:last-child{opacity:.3}
js
const root=document.querySelector('.r2'),caption=root.querySelector('.r2-caption'),progress=root.querySelector('.r2-progress'),labels=["Function requires Display","Only implementing types pass","Generic call can format"];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()})

A Rust bound such as T: Display lets a generic function use behavior promised by the trait. Callers can pass only types that implement that trait.

The demo’s format function requires Display. String passes, while a hypothetical Photo without a Display implementation is rejected. The bound names a capability without fixing the function to one concrete type.

When to use

Use trait bounds in generic APIs that accept many types but require behavior such as formatting or comparison.

Open as page ↗