|
|
Subscribe / Log in / New account

Are casts encouraged in Rust?

Are casts encouraged in Rust?

Posted Jun 30, 2025 7:38 UTC (Mon) by tialaramex (subscriber, #21167)
In reply to: Are casts encouraged in Rust? by alx.manpages
Parent article: How to write Rust in the kernel: part 2

Lots of useful ideas, perhaps beyond what a novice Rust programmer would realise, are trapped behind the problem that trait implementations in Rust aren't today able to be marked constant.

An example beyond what you might expect is that for-each loops can't be constant. Rust's for-each loops always de-sugar into use of the trait call IntoIterator::into_iter to make whatever you've got into an iterator to begin with. This happens even if what you've provided is already an iterator, such conversion is just the identity function so your optimiser will elide the work - so until that trait implementation can be constant itself, the entire for-each loop feature isn't available in constants. You can write a while loop, for example, and that works fine, but not a for-each loop.


to post comments


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds