# Ep. 4: Type Coercion in Rust: Ensuring Safety and Preventing Data Loss

DevFeed: [Ep. 4: Type Coercion in Rust: Ensuring Safety and Preventing Data Loss](<https://devfeed.tech/articles/ep-4-type-coercion-in-rust-ensuring-safety-and-preventing-data-loss-22240.md>)

Original publisher: [Read original article](<https://www.ardanlabs.com/blog/2024/05/type-coercion-in-rust-ep-4.html>)

Published: 2024-05-03T00:00:00Z

Content type: article

Language: en

Sources: [William Kennedy](<https://devfeed.tech/sources/william-kennedy.md>)

Topics: [type coercion](<https://devfeed.tech/topics/type-coercion.md>), [Rust](<https://devfeed.tech/topics/rust.md>), [typesafety](<https://devfeed.tech/topics/typesafety.md>), [data type](<https://devfeed.tech/topics/data-type.md>)

Tags: [c](<https://devfeed.tech/tags/c.md>), [code](<https://devfeed.tech/tags/code.md>), [data-type](<https://devfeed.tech/tags/data-type.md>), [data-types](<https://devfeed.tech/tags/data-types.md>), [developers](<https://devfeed.tech/tags/developers.md>), [errors](<https://devfeed.tech/tags/errors.md>), [go](<https://devfeed.tech/tags/go.md>), [integrity](<https://devfeed.tech/tags/integrity.md>), [memory-handling](<https://devfeed.tech/tags/memory-handling.md>), [memory-safety](<https://devfeed.tech/tags/memory-safety.md>), [performance](<https://devfeed.tech/tags/performance.md>), [programming](<https://devfeed.tech/tags/programming.md>), [programming-languages](<https://devfeed.tech/tags/programming-languages.md>), [reliability](<https://devfeed.tech/tags/reliability.md>), [risks](<https://devfeed.tech/tags/risks.md>), [robust](<https://devfeed.tech/tags/robust.md>), [rust](<https://devfeed.tech/tags/rust.md>), [safety](<https://devfeed.tech/tags/safety.md>), [safety-concerns](<https://devfeed.tech/tags/safety-concerns.md>), [software](<https://devfeed.tech/tags/software.md>), [type-coercion](<https://devfeed.tech/tags/type-coercion.md>), [types](<https://devfeed.tech/tags/types.md>), [values](<https://devfeed.tech/tags/values.md>)

## AI overview

The episode explains how implicit type coercion can produce misleading results, using a C example, and says Go and Rust make assignments between incompatible types difficult. It notes that converting smaller values to larger types requires explicit casts in both languages. For Rust, it highlights the Into and TryInto traits for conversions, with TryInto handling possible errors, and stresses checking conversions to help protect code correctness. This overview is based on the supplied excerpt.

## Source excerpt

Introduction: Delving into the intricate realm of type coercion, Herbert's insights offer invaluable guidance for Rust developers grappling with the complexities of data type conversions: Gain insights into the nuances of type coercion, a fundamental aspect of programming languages, and its potential pitfalls, drawing parallels with numerical computation surprises. Explore how Go and Rust tackle inadvertent type coercion, prioritizing program integrity by making it challenging to assign values between incompatible types.