Are casts encouraged in Rust?
Are casts encouraged in Rust?
Posted Jun 29, 2025 19:37 UTC (Sun) by alx.manpages (subscriber, #145117)Parent article: How to write Rust in the kernel: part 2
const BMCR_SPEED100: u16 = uapi::BMCR_SPEED100 as u16;
sounds like C's
constexpr int16_t BMCR_SPEED100 = (int16_t) BMCR_SPEED100;
Casts are strongly discouraged in C due to the silence they impose to compiler diagnostics about value change during conversion (they can drop bits). It is common to want a total amount of zero casts in a program, when possible (and often it is possible). Are casts not a problem in Rust?
