Codex Choose Rust Generics or dyn Trait for Formatter Dispatch
Compare static and dynamic dispatch with a compilable example and decide when heterogeneous collections are needed.
中文Install in Codex
Use a local Rust project, Cargo, and the target trait and implementations.
- Copy the pinned `.codex/skills/rust-zero-cost/` directory into `.agents/skills/rust-zero-cost/` in the project.
- Tell Codex whether the concrete type is known at compile time and check that the trait supports `dyn`.
Example task
Define `LabelFormatter` with uppercase and identity implementations. Call a generic function with a known type and store both implementations in `Vec<Box<dyn LabelFormatter>>`.What to check
The heterogeneous list dispatches to the two implementations correctly, and tests preserve the static call's output.
Original SKILL.md on GitHub · Updated
Frequently asked question
Are generics always faster?
Measure the actual path, including allocations and code size, before making a performance claim.