this post was submitted on 29 Jul 2023
8 points (90.0% liked)
Rust
5953 readers
8 users here now
Welcome to the Rust community! This is a place to discuss about the Rust programming language.
Wormhole
Credits
- The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Discriminant is irrelevant and you're not supposed to fuck with it.
And there is zero reason to use unsafe/transmute for this.
And just to explicitly point out, your code's also better because of the use of the standard traits. It took me a while to get into the habit, but using what's already there is always a good idea.
It matters because the conversion between i32 and the Result is only "free" if they have the same layout (which they do not, because of the discriminant). So a more costly conversion method is required.
You are right, because the compiler is able to optimize your code quite well. However, if that optimization were to break at some point (as there is no guarantee that an optimization will continue to work in the future), it would become less efficient.
That seems like strong premature optimisation. Perhaps worth a note, but I'd presume the majority of people the majority of the time wouldn't need to worry about that.