---
title: "Rust Traits vs. Java/C# Interfaces: Shared Behavior Done Right"
description: "Discussion on Rust traits vs Java/C# interfaces, covering dispatch mechanisms, compile-time behavior, and performance optimizations."
url: "https://mayorana.ch/en/blog/rust-traits-vs-interfaces"
language: "en"
date: "2025-07-04"
author: "Mohamed Bennekrouf"
tags:
  - "rust"
  - "traits"
fr_version: "https://mayorana.ch/fr/blog/rust-traits-vs-interfaces.md"
---

# Rust Traits vs. Java/C# Interfaces: Shared Behavior Done Right

Rust traits and interfaces both define shared behavior, but differ fundamentally in design and execution, especially in performance-critical contexts.

<div class="svg-container" style="margin:2rem 0;">
<svg class="td1-fig" viewBox="0 0 800 300" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="Comparison of Rust static dispatch versus Java or C# dynamic dispatch for a PacketHandler call">
<style>
.td1-fig{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .td1-fig,[data-theme="dark"] .td1-fig{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.td1-fig .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.td1-fig .boxAc{fill:var(--box);stroke:var(--ac);stroke-width:2}
.td1-fig .tx{fill:var(--tx);font:600 12px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td1-fig .ti{fill:var(--tx);font:700 14px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td1-fig .mut{fill:var(--mut);font:600 11px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td1-fig .ln{stroke:var(--ln);stroke-width:1.5;fill:none}
</style>
<!-- markers -->
<defs>
<marker id="td1-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>
</defs>
<!-- column titles -->
<text x="200" y="26" class="ti">Rust: static dispatch</text>
<text x="600" y="26" class="ti">Java/C#: dynamic dispatch</text>
<!-- left column -->
<rect class="box" x="60" y="45" width="280" height="42" rx="6"/>
<text x="200" y="70" class="tx">process_packets::&lt;TcpHandler&gt;()</text>
<path class="ln" d="M200,87 L200,113" marker-end="url(#td1-arrow)"/>
<rect class="box" x="60" y="114" width="280" height="42" rx="6"/>
<text x="200" y="139" class="tx">Monomorphized per type</text>
<path class="ln" d="M200,156 L200,182" marker-end="url(#td1-arrow)"/>
<rect class="boxAc" x="60" y="183" width="280" height="52" rx="6"/>
<text x="200" y="204" class="tx">Inlined machine code</text>
<text x="200" y="222" class="mut">zero runtime overhead</text>
<!-- right column -->
<rect class="box" x="460" y="45" width="280" height="42" rx="6"/>
<text x="600" y="70" class="tx">process(&amp;mut self, data)</text>
<path class="ln" d="M600,87 L600,113" marker-end="url(#td1-arrow)"/>
<rect class="box" x="460" y="114" width="280" height="42" rx="6"/>
<text x="600" y="139" class="tx">vtable lookup</text>
<path class="ln" d="M600,156 L600,182" marker-end="url(#td1-arrow)"/>
<rect class="box" x="460" y="183" width="280" height="52" rx="6"/>
<text x="600" y="204" class="tx">Indirect call</text>
<text x="600" y="222" class="mut">1-2 cycles, no inlining</text>
<!-- captions -->
<text x="200" y="265" class="mut">Zero-cost abstraction</text>
<text x="600" y="265" class="mut">Runtime polymorphism</text>
</svg>
</div>

## Traits and interfaces, side by side
| Aspect | Rust Traits | Java/C# Interfaces |
|--------|-------------|-------------------|
| **Dispatch** | Static dispatch (generics) by default, opt-in dynamic (`dyn`) | Runtime polymorphism via vtables |
| **Implementation** | Explicit via `impl Trait for Type` | Implicit (C#) or explicit (Java) |
| **Compile-time** | Resolved at compile time via monomorphization | Runtime constructs with JIT optimization |
| **Inheritance** | No inheritance; composition via supertraits | Interface inheritance with runtime checks |
| **Performance** | Zero-cost abstraction, inlining enabled | 1-2 cycle dispatch cost, limited inlining |

## Implementation and Dispatch

**Rust Traits**: Support [static dispatch](https://mayorana.ch/en/blog/dispatch-performance-rust) via generics where the compiler monomorphizes code for each type, inlining calls for zero runtime overhead. Dynamic dispatch (`dyn Trait`) uses vtables but is opt-in.

**Java/C# Interfaces**: Rely on runtime polymorphism via vtables, incurring dispatch costs and preventing inlining across type boundaries.

## Example: Performance-Critical Networking Stack

Define a `PacketHandler` trait for efficient packet processing across different protocols:

```rust
trait PacketHandler {
    fn process(&mut self, data: &[u8]) -> usize; // Bytes processed
    fn reset(&mut self); // Reset state
}

struct TcpHandler { state: u32 }
struct UdpHandler { count: u16 }

impl PacketHandler for TcpHandler {
    fn process(&mut self, data: &[u8]) -> usize {
        self.state = data.iter().fold(self.state, |acc, &x| acc.wrapping_add(x as u32));
        data.len()
    }
    fn reset(&mut self) { self.state = 0; }
}

impl PacketHandler for UdpHandler {
    fn process(&mut self, data: &[u8]) -> usize {
        self.count = self.count.wrapping_add(1);
        data.len()
    }
    fn reset(&mut self) { self.count = 0; }
}

fn process_packets<H: PacketHandler>(handler: &mut H, packets: &[&[u8]]) -> usize {
    let mut total = 0;
    for packet in packets {
        total += handler.process(packet);
    }
    total
}
```

Usage:
```rust
let mut tcp = TcpHandler { state: 0 };
let packets = vec![&[1, 2, 3], &[4, 5, 6]];
let bytes = process_packets(&mut tcp, &packets); // Static dispatch
```

## How It Enhances Performance and Safety

### Performance

- **Static Dispatch**: `process_packets` monomorphizes for `TcpHandler` and `UdpHandler`, generating separate, inlined code paths. No vtable lookups, saving cycles in hot loops
- **Inlining**: Compiler can inline `process` calls, fusing them with the loop, reducing branches and enabling [SIMD](https://mayorana.ch/en/blog/simd-matrix-mult-rust) optimizations
- **Zero-Cost**: Trait abstraction adds no runtime overhead—equivalent to hand-writing `process_tcp` and `process_udp`

### Safety

- **Type Safety**: [Trait bound](https://mayorana.ch/en/blog/trait-bounds-rust) `H: PacketHandler` ensures only compatible types are passed, checked at compile time—no runtime casts like Java's `instanceof`
- **Encapsulation**: Each handler manages its state (`state` or `count`), with Rust's ownership enforcing mutation rules

<div class="svg-container" style="margin:2rem 0;">
<svg class="td1-fig2" viewBox="0 0 800 300" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="The H PacketHandler bound is checked at compile time, routing to monomorphized inlined code or to a compile error before any binary exists">
<style>
.td1-fig2{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .td1-fig2,[data-theme="dark"] .td1-fig2{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.td1-fig2 .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.td1-fig2 .boxAc{fill:var(--box);stroke:var(--ac);stroke-width:2}
.td1-fig2 .tx{fill:var(--tx);font:600 12px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td1-fig2 .ti{fill:var(--tx);font:700 14px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td1-fig2 .mut{fill:var(--mut);font:600 11px ui-sans-serif,system-ui,sans-serif;text-anchor:middle}
.td1-fig2 .ln{stroke:var(--ln);stroke-width:1.5;fill:none}
.td1-fig2 .lnAc{stroke:var(--ac);stroke-width:2;fill:none}
</style>
<!-- markers -->
<defs>
<marker id="td1b-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="td1b-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>
<!-- title -->
<text x="400" y="26" class="ti">Where a wrong handler type gets caught</text>
<!-- call site -->
<rect class="box" x="40" y="110" width="210" height="60" rx="6"/>
<text x="145" y="136" class="tx">process_packets(&amp;mut tcp)</text>
<text x="145" y="156" class="mut">H inferred = TcpHandler</text>
<!-- arrow into gate -->
<path class="lnAc" d="M250,140 L300,140" marker-end="url(#td1b-arrowAc)"/>
<!-- gate -->
<rect class="boxAc" x="302" y="105" width="200" height="70" rx="6"/>
<text x="402" y="132" class="tx">H: PacketHandler ?</text>
<text x="402" y="152" class="mut">resolved at compile time</text>
<!-- split from gate -->
<path class="ln" d="M502,140 L532,140"/>
<path class="ln" d="M532,140 L532,70 L560,70" marker-end="url(#td1b-arrow)"/>
<path class="ln" d="M532,140 L532,215 L560,215" marker-end="url(#td1b-arrow)"/>
<!-- accepted -->
<rect class="box" x="562" y="45" width="208" height="50" rx="6"/>
<text x="666" y="68" class="tx">impl found</text>
<text x="666" y="85" class="mut">monomorphize, then inline</text>
<!-- rejected -->
<rect class="box" x="562" y="190" width="208" height="50" rx="6"/>
<text x="666" y="213" class="tx">no impl for that type</text>
<text x="666" y="230" class="mut">error[E0277] — nothing is built</text>
<!-- caption -->
<text x="400" y="275" class="mut">Java: the same mistake compiles and surfaces as an instanceof check or a cast failure at run time</text>
</svg>
</div>

## Contrast with Java/C#

Java equivalent:
```java
interface PacketHandler {
    int process(byte[] data);
    void reset();
}

class TcpHandler implements PacketHandler {
    // vtable-based dispatch, no inlining across types
}
```

Every `process` call goes through a vtable, preventing loop fusion and adding indirection. Rust's static dispatch avoids this—critical for networking stacks handling millions of packets per second.

## Advanced Considerations

- **[Associated Types](https://mayorana.ch/en/blog/associated-types-io-driver-api)**: Enable type-level constraints without runtime overhead
- **Default Implementations**: Reduce boilerplate while maintaining zero-cost
- **[Supertraits](https://mayorana.ch/en/blog/supertraits-bounds-optimization)**: Compose behavior without inheritance complexity
- **Dynamic Dispatch**: Use `Box<dyn PacketHandler>` when type erasure is needed

## Traits vs interfaces, in short
**Rust traits**: Compile-time resolution, [zero-cost abstraction](https://mayorana.ch/en/blog/zero-cost-abstractions-rust), static dispatch by default  
**Java/C# interfaces**: Runtime polymorphism, vtable overhead, dynamic by nature  
Use traits for performance-critical code where static dispatch eliminates overhead

Switching to `&dyn PacketHandler` buys you a smaller binary and one implementation of the
function, and costs you an indirect call the optimizer can't see through. Whether that
matters depends entirely on how hot the path is — so measure before you decide.
