#traits Clear
Tags #php #kotlin #bash #go #sql #rust #typescript #html #java #python #files #utils #strings #http #concurrency #async #json #arrays #security #types #crypto #database #dates #format
Rust impl Trait — Static Dispatch Returns
When you want to return a complex iterator chain or closure, you don't need to spell out the type. `impl Trait` lets the compiler infer the concrete type while exposing only the API contract.
Rust Trait Objects (dyn Trait)
When you need a heterogeneous collection — different concrete types behind one trait — use `Box<dyn Trait>`. Costs one indirection (vtable) but enables polymorphism that `impl Trait` can't.
Rust Trait with Default Methods
A trait can supply default implementations — implementors override only the bits they need. Like abstract base classes, but with structural typing and zero runtime cost.
Rust Generic Function with Trait Bounds
Use `T: Trait` to require a capability on a generic parameter. The `where` clause is the more readable variant when bounds get long.
Rust From / Into Conversions
Implement `From` and you get `Into` for free. Then `.into()` and `T::from(x)` both work. The single most idiomatic way to express type conversions in Rust.
Rust Custom Display + Debug Implementation
`Debug` (for `{:?}`) is usually derived. `Display` (for `{}`) is hand-written and goes through the same `std::fmt::Formatter` API.