4 ms·
What's the difference between constant and immutable?
by hardlianotion 3y ago
What's the difference between constant and immutable?
- layer8 3y ago“Const” doesn’t mean immutable in C++, in my book. It means unmodifiable or read-only. Immutable means the value won’t ever change, whereas unmodifiable means that the value can’t be modified via that type (but it might be modified some other way).
- raincole 3y agoCompile time vs runtime.
- GolDDranks 3y agoConstant is determined at compile time. It's constant over the every run of the same binary. Immutable is immutable for some specific span of time. In a simple case, like with Python immutable objects, it is constructed once, and after that, it never changes. With Rust, it's also possible for an object to be immutable while a reference to it exists and is passed to some function, but after that function call returns and the reference ceases existing, immutability ends.
- estebank 3y agoTo add to this, the following is valid: fn take_foo(x: Foo) -> Foo { let mut x = x; x.field = 0; x } The ownership semantics ensure that because there's a single owner of Foo at all times, it is always safe to make its binding mutable. This is different than trying to turn a &Foo into a &mut Foo, that's not allowed by safe Rust (and an undefined behavior minefield in unsafe Rust). For constants, you can think of them as a "stamp", whatever the value within the const gets inlined in the usage place in your code, every time. Immutability is about whether the value can be changed. To stretch the analogy, you can stamp a pattern on paper (const) but later doodle over it (mutability).
- tialaramex 3y agoAn example of a constant would be a literal, such as "Hello, World!" or 4 or false You can see that not only can't you change these values, it doesn't even mean anything to speak about doing so, they're not variables which have values, they're the values themselves. Whereas an example of an immutable variable would be the std::vector we're being asked to check the length of in a call to size() - this certainly is a variable, somebody else can change it, but we must not. If we name a constant in Rust like { const TIMEOUT: u32 = 1234; }, we're not saying we want a variable with this name TIMEOUT and then we mustn't change it - instead we are giving a name to this arbitrary constant TIMEOUT (and it has to be constant, Rust won't let us make constants unless they actually are) and then any time we say that name, we get the same value, not the same variable, there is no variable, the same value, the constant one, as if we'd written it here. So for example the C++ trick where you "cast away" the const-ness doesn't make any sense in Rust because those aren't immutable variables, we can't even try to make them mutable, they're not variables. It's not Undefined Behaviour, it's gibberish, even C++ won't let you write false = true; for example, that's just clearly nonsense.
- soulbadguy 3y ago> An example of a constant would be a literal, such as "Hello, World!" or 4 or false Not quite, those are literals values. As mentioned in the C++ standards, const-ness is a quality of variables... Not of values.
- teo_zero 3y agoBut that's exactly the criticism: that C++ used the word const for something different from its theoretical meaning, something that could have been better expressed with the word immutable.