Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Why is rust allowed to reorder fields? If I know that fields are going to be generally accessed together, this prevents me from ordering them so they fit in cache lines.
 help



You can choose in Rust to explain the representation you want for your data type. Unlike C or C++ that's not a non-portable vendor extension it's just part of the language, look at the repr documentation: https://doc.rust-lang.org/nomicon/other-reprs.html

So if you want "what C does" you can just repr(C) and that's what you get. For most people that's not a good trade unless they're doing FFI with a language that shares this representational choice.


It's allowed as an optimization, the order it uses will limit the space lost to field alignment.

You can tell it not to reorder them if you want but it's not the default.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: