---
title: "Trait Bounds"
description: "Using trait bounds in Rust for type safety and performance in mathematical computations"
url: "https://mayorana.ch/en/blog/trait-bounds-rust"
language: "en"
date: "2025-08-11"
author: "Mohamed Bennekrouf"
tags:
  - "rust"
  - "generics"
  - "trait-bounds"
  - "monomorphization"
  - "performance"
fr_version: "https://mayorana.ch/fr/blog/trait-bounds-rust.md"
---

# Trait Bounds

In a performance-sensitive Rust library for mathematical computations, trait bounds like `T: Add + Mul` ensure type safety and maximize performance by restricting generic types to those supporting required operations, enabling efficient, type-specific code via monomorphization.

<div class="svg-container" style="margin:2rem 0;">
<svg class="td2-fig" viewBox="0 0 800 280" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="Flow from a bounded generic dot_product function through monomorphization to specialized f32 and i32 machine code">
<style>
.td2-fig{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .td2-fig,[data-theme="dark"] .td2-fig{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.td2-fig .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.td2-fig .boxAc{fill:var(--box);stroke:var(--ac);stroke-width:2}
.td2-fig .tx{fill:var(--tx);font:600 12px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td2-fig .ti{fill:var(--tx);font:700 14px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td2-fig .mut{fill:var(--mut);font:600 11px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td2-fig .ln{stroke:var(--ln);stroke-width:1.5;fill:none}
.td2-fig .lnAc{stroke:var(--ac);stroke-width:2;fill:none}
</style>
<!-- markers -->
<defs>
<marker id="td2-arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,0L10,5L0,10z" fill="var(--ln)"/></marker>
<marker id="td2-arrowAc" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,0L10,5L0,10z" fill="var(--ac)"/></marker>
</defs>
<!-- generic function -->
<rect class="box" x="240" y="20" width="320" height="46" rx="6"/>
<text x="400" y="42" class="tx">dot_product&lt;T: Add+Mul+Default+Copy&gt;</text>
<text x="400" y="58" class="mut">where T::Output = T</text>
<!-- arrow down to monomorphization -->
<path class="lnAc" d="M400,66 L400,92" marker-end="url(#td2-arrowAc)"/>
<rect class="boxAc" x="280" y="93" width="240" height="40" rx="6"/>
<text x="400" y="118" class="tx">Monomorphization</text>
<!-- Y-merge split to two specialized versions -->
<path class="ln" d="M400,133 L400,150 L220,150 L220,168" marker-end="url(#td2-arrow)"/>
<path class="ln" d="M400,150 L580,150 L580,168" marker-end="url(#td2-arrow)"/>
<!-- f32 box -->
<rect class="box" x="80" y="169" width="280" height="70" rx="6"/>
<text x="220" y="192" class="tx">f32 version</text>
<text x="220" y="210" class="mut">fldz / fmul / fadd</text>
<text x="220" y="226" class="mut">inlined, no calls</text>
<!-- i32 box -->
<rect class="box" x="440" y="169" width="280" height="70" rx="6"/>
<text x="580" y="192" class="tx">i32 version</text>
<text x="580" y="210" class="mut">xor / imul / add</text>
<text x="580" y="226" class="mut">inlined, no calls</text>
<!-- caption -->
<text x="400" y="265" class="mut">One generic body, separate native code paths per T — no vtable</text>
</svg>
</div>

## Example: Dot Product Function

Consider a dot product function for two vectors, critical in signal processing or machine learning:

```rust
use std::ops::{Add, Mul};

fn dot_product<T>(a: &[T], b: &[T]) -> T
where
    T: Add<Output = T> + Mul<Output = T> + Default + Copy,
{
    assert_eq!(a.len(), b.len());
    let mut sum = T::default();
    for i in 0..a.len() {
        sum = sum + (a[i] * b[i]);
    }
    sum
}

// Usage
fn main() {
    let v1 = vec![1.0, 2.0, 3.0];
    let v2 = vec![4.0, 5.0, 6.0];
    let result = dot_product(&v1, &v2); // 32.0 (1*4 + 2*5 + 3*6)
    println!("{}", result);
}
```

## Applying Trait Bounds

- `T: Add<Output = T>`: Ensures `T` supports `+` and returns `T`, allowing `sum + ...`.
- `T: Mul<Output = T>`: Ensures `T` supports `*` and returns `T`, enabling `a[i] * b[i]`.
- `T: Default`: Provides a zero-like starting value for `sum`, common for numeric types.
- `T: Copy`: Allows stack-based copying of `T` values (e.g., `a[i]`), avoiding costly cloning or references for primitives like `f32`.

<div class="svg-container" style="margin:2rem 0;">
<svg class="td2b-fig" viewBox="0 0 800 290" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="Each trait bound on dot_product maps to the one line of the function body it makes legal">
<style>
.td2b-fig{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .td2b-fig,[data-theme="dark"] .td2b-fig{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.td2b-fig .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.td2b-fig .boxAc{fill:var(--box);stroke:var(--ac);stroke-width:2}
.td2b-fig .tx{fill:var(--tx);font:600 12px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td2b-fig .ti{fill:var(--tx);font:700 14px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td2b-fig .mut{fill:var(--mut);font:600 11px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td2b-fig .ln{stroke:var(--ln);stroke-width:1.5;fill:none}
.td2b-fig .lnAc{stroke:var(--ac);stroke-width:2;fill:none}
</style>
<!-- markers -->
<defs>
<marker id="td2b-arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,0L10,5L0,10z" fill="var(--ln)"/></marker>
<marker id="td2b-arrowAc" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,0L10,5L0,10z" fill="var(--ac)"/></marker>
</defs>
<!-- headers -->
<text x="165" y="24" class="ti">Bound</text>
<text x="595" y="24" class="ti">The line it makes legal</text>
<!-- row 1: Add -->
<rect class="box" x="40" y="44" width="250" height="40" rx="6"/>
<text x="165" y="69" class="tx">T: Add&lt;Output = T&gt;</text>
<path class="ln" d="M290,64 L428,64" marker-end="url(#td2b-arrow)"/>
<rect class="box" x="430" y="44" width="330" height="40" rx="6"/>
<text x="595" y="69" class="tx">sum = sum + …  (stays T)</text>
<!-- row 2: Mul -->
<rect class="box" x="40" y="96" width="250" height="40" rx="6"/>
<text x="165" y="121" class="tx">T: Mul&lt;Output = T&gt;</text>
<path class="ln" d="M290,116 L428,116" marker-end="url(#td2b-arrow)"/>
<rect class="box" x="430" y="96" width="330" height="40" rx="6"/>
<text x="595" y="121" class="tx">a[i] * b[i]</text>
<!-- row 3: Default -->
<rect class="box" x="40" y="148" width="250" height="40" rx="6"/>
<text x="165" y="173" class="tx">T: Default</text>
<path class="ln" d="M290,168 L428,168" marker-end="url(#td2b-arrow)"/>
<rect class="box" x="430" y="148" width="330" height="40" rx="6"/>
<text x="595" y="173" class="tx">let mut sum = T::default()</text>
<!-- row 4: Copy -->
<rect class="boxAc" x="40" y="200" width="250" height="40" rx="6"/>
<text x="165" y="225" class="tx">T: Copy</text>
<path class="lnAc" d="M290,220 L428,220" marker-end="url(#td2b-arrowAc)"/>
<rect class="boxAc" x="430" y="200" width="330" height="40" rx="6"/>
<text x="595" y="225" class="tx">read a[i] with no move, no clone</text>
<!-- caption -->
<text x="400" y="270" class="mut">Remove one bound and exactly that line stops compiling — nothing waits until run time</text>
</svg>
</div>

## Ensuring Type Safety

- **Compile-Time Checks**: The bounds reject invalid types at compile time. For example:
  ```rust
  let strings = vec!["a", "b"];
  dot_product(&strings, &strings); // Error: String doesn’t implement Add/Mul
  ```
  This prevents runtime errors, crucial for a library where users supply diverse types.
- **Correctness**: `Output = T` ensures operations chain without type mismatches (e.g., no unexpected `Option` or `Result`).

## Ensuring Performance

- **[Static Dispatch](https://mayorana.ch/en/blog/dispatch-performance-rust)**: The bounds enable static dispatch via generics. The compiler monomorphizes `dot_product` for each `T`, generating specialized code (e.g., one for `f32`, another for `i32`).
- **Inlining**: Small operations like `+` and `*` (from `Add` and `Mul`) are inlined, reducing call overhead and enabling loop optimizations (e.g., unrolling or [SIMD](https://mayorana.ch/en/blog/simd-matrix-mult-rust) if `T` is a primitive).
- **No Abstraction Overhead**: Unlike `dyn Trait`, there’s no vtable—pure machine code tailored to `T`.

## Impact on Monomorphization

Monomorphization [duplicates](https://mayorana.ch/en/blog/efficient-duplicate-removal-vec) the generic function for each concrete type used:

- **For `f32`**:
  ```asm
  ; Pseudocode assembly
  fldz                ; sum = 0.0
  loop:
    fld [rsi + rax*4] ; Load a[i]
    fmul [rdi + rax*4]; Multiply with b[i]
    fadd st(0), st(1) ; Add to sum
    inc rax
    cmp rax, rcx
    jl loop
  ```

- **For `i32`**:
  ```asm
  xor eax, eax       ; sum = 0
  loop:
    mov ebx, [rsi + rcx*4] ; Load a[i]
    imul ebx, [rdi + rcx*4]; Multiply with b[i]
    add eax, ebx       ; Add to sum
    inc rcx
    cmp rcx, rdx
    jl loop
  ```

**Result**: Each version uses native instructions for `T`’s operations, with no runtime type checks or indirection.

## Trade-Offs and Considerations

- **Code Size**: Monomorphization increases binary size (e.g., separate code for `f32`, `i32`, `f64`). In a library with many types or functions, this could bloat the executable, potentially harming instruction cache efficiency.
- **Compile Time**: More monomorphized instances mean longer builds, though this is a one-time cost.
- **Mitigation**: Use bounds judiciously—e.g., `T: Copy` avoids references for primitives but excludes complex types. For broader use, consider `T: Clone` as an alternative, with a performance trade-off.

## Verification

- **Benchmark**: Use `criterion` to confirm performance:
  ```rust
  use criterion::{black_box, Criterion};
  fn bench(c: &mut Criterion) {
      let v1 = vec![1.0_f32; 1000];
      let v2 = vec![2.0_f32; 1000];
      c.bench_function("dot_product_f32", |b| b.iter(|| dot_product(black_box(&v1), black_box(&v2))));
  }
  ```
  Expect tight, consistent times (e.g., 1µs) due to inlining and native ops.
- **Assembly**: `cargo rustc --release -- --emit asm` shows optimized loops, no calls.

## Conclusion
Trait bounds like `T: Add + Mul + Default + Copy` in `dot_product` enforce safety (only numeric types) and performance (static, inlined code). Monomorphization turns this into type-specific machine code, ideal for a math library. Balancing these bounds ensures a flexible yet efficient API, with profiling to avoid hidden costs.
