16.1 Rust’s Philosophy: Explicit and Safe Conversions
In systems programming, manipulating data across different types is fundamental. C often performs implicit conversions, sometimes unexpectedly. Rust, conversely, mandates that type changes be explicit in the code, enhancing clarity and preventing errors.
Rust’s core principles regarding type conversions are:
- Explicitness: Type conversions must be clearly requested by the programmer using specific syntax or trait methods. Rust generally avoids implicit coercions between distinct types (with specific exceptions like lifetime elision or deref coercions, which are different from casting).
- Safety: Conversions that could potentially fail or lose information are designed to make the possibility of failure explicit. Fallible conversions typically return a
Result
, forcing the programmer to handle potential errors instead of risking silent data corruption or undefined behavior common in C/C++.
16.1.1 Categories of Conversions
Rust categorizes conversions primarily by whether they can fail:
- Primitive Casting (
as
): A direct, low-level cast primarily for primitive types and raw pointers. It performs no runtime checks and can silently truncate, saturate, or change value interpretation. Use requires programmer awareness of the consequences. - Infallible Conversions (
From
/Into
): Implemented via theFrom<T>
andInto<U>
traits. These conversions are guaranteed to succeed and represent idiomatic, safe type transformations (e.g., widening an integer likeu8
tou16
). ImplementingFrom<T> for U
automatically providesInto<U>
forT
. - Fallible Conversions (
TryFrom
/TryInto
): Implemented via theTryFrom<T>
andTryInto<U>
traits. These conversions return aResult<TargetType, ErrorType>
, indicating that the conversion might not succeed (e.g., narrowing an integer likei32
toi8
, parsing a string). ImplementingTryFrom<T> for U
automatically providesTryInto<U>
forT
. - Unsafe Bit Reinterpretation (
transmute
): Thestd::mem::transmute
function reinterprets the raw bits of one type as another type of the same size. It is highly unsafe and bypasses the type system entirely.