Understand Type Conversions in Rust
masterRust provides several ways to obtain a type B from a type A. These methods range from trivial identity to complex automatic conversions like coercions and subtyping.
Methods include:
- Identity:
Bis exactlyA. - Computation: Writing code to transform data from
AtoB. - Casts: On-demand conversion using the
askeyword (use with caution). - Coercions: Automatic conversion within a 'weakening ruleset' (e.g., converting a mutable reference to an immutable one).
- Subtyping: Automatic conversion within a 'same-layout-different-lifetimes ruleset'.