this post was submitted on 06 Nov 2021
21 points (92.0% liked)

Rust Programming

8438 readers
26 users here now

founded 5 years ago
MODERATORS
top 4 comments
sorted by: hot top controversial new old
[–] [email protected] 3 points 3 years ago (1 children)

Hey I'm a rust noob. What's the difference between a const and the default immutable variable?

[–] [email protected] 2 points 3 years ago* (last edited 3 years ago)

Also noob here (so anyone that knows better can correct me), they have an entire section in The Book dedicated to explaining the differences, but basically those differences are:

  • contants are completely immutable, unlike variables, which are only immutable by default and can be changed using mut;
  • unlike variables, contants can be used in any scope of your code, so if you need some data that needs to be read by many parts of your code, and not just on your main function, it's way better to use it;
  • since they are constant, as the name suggests, they can only hold expressions or values that can be computed durinig compilation, so for example, it can take an expression like 2.0 * 3.1415 / 4.0 but can't use variables or other stuff that is processed during runtime, like, x + 14 / y or add(4, 3) (just an example of a function, in this case).
[–] [email protected] 0 points 2 years ago (1 children)

This aged poorly...

Wish we had const floats too.

[–] [email protected] 1 points 2 years ago

What do you mean by this? I'm pretty sure you can make a const float.