16.1 Introduction to Type Conversions
16.1.1 Implicit vs. Explicit Conversions
In many programming languages, type conversions can occur implicitly. For example, integers might automatically be converted to floating-point numbers during arithmetic operations. Rust, however, does not perform implicit type conversions. This design choice ensures type safety and makes all conversions explicit, requiring the developer to clearly indicate when a type transformation occurs.
16.1.2 Rust’s Philosophy on Type Safety
Rust’s strict type system prioritizes safety and clarity. Conversions between types must either:
- Be explicitly requested, such as with the
as
keyword or theInto
andFrom
traits. - Be designed to handle potential errors explicitly, such as with
TryFrom
andTryInto
.
This philosophy helps avoid subtle bugs caused by unintended type coercion.