name: cairo-deref-coercion description: Explain Cairo deref coercion with Deref/DerefMut traits and Target associated type; use when a request involves wrapper types, field access through wrappers, or deref behavior in Cairo.
Cairo Deref Coercion
Overview
Guide how deref coercion works via Deref and DerefMut, and when it applies.
Quick Use
- Read
references/deref-coercion.mdbefore answering. - Show the
Dereftrait and a wrapper example. - Note that
DerefMutonly applies to mutable variables.
Response Checklist
- Implement
Deref<T>withtype Targetandfn deref(self: T) -> Target. - Use deref coercion to access fields on the wrapped type.
- Use
DerefMutwhen you need mutable-only coercion; it does not make the target mutable by itself.
Example Requests
- "Why can I access fields on a wrapper type?"
- "How do I implement Deref for a custom type?"
- "Why does DerefMut require a mut variable?"