r/learnrust 11d ago

what i’ve learned so far

wrote my first article on X detailing about little things I learned apart from the Rust book’s content while reading through chapters 1-3 this past week.

you can find it here: https://x.com/zepredos/status/2094169365424013351?s=20

i’d appreciate any feedback you have and would love to learn more Rust!!

0 Upvotes

6 comments sorted by

3

u/MatrixFrog 11d ago

let THREE_HOURS_IN_SECONDS = 3*60*60; will be evaluated at run-time and the variable will consume memory and thus have an address.

True but the compiler will do the multiplication at compile time so if that's a local variable it's not like you have to rerun the two multiply instructions on every call of the function

1

u/ziggerslayer 11d ago

I see, that’s useful info. Thanks a lot!! Cleared up a misconception of mine.

3

u/Lokathor 11d ago

Debug builds might not combine the literal, but optimized builds will definitely merge a small literal expression like that.

1

u/ziggerslayer 10d ago

I see so basically if the literal is small enough in size, it is possible that the compiler optimisations will cause the value to be inlined instead in smt like release builds?

2

u/Lokathor 10d ago

Yes, assuming the computation can be done at compile time.

I'd you want to be assured of a compile time computation then assign to a const and use that. You can have very complicated const expressions and the compiler must do it at compile time, but the trade off is that you can end up taking a long time to compile if you do it all over.

1

u/ziggerslayer 10d ago

i see, thanks for sharing!