Constants and variables


#1

Hi! Im a total newbee in Rust, reading the http://doc.rust-lang.org/book/variable-bindings.html. Maybe it is too early to ask but what is the difference between constansts and variables since I cant change the variable value without adding “mut” at the binding? Why it is called variable?


#2

constants are just compile time values. They don’t neccessarily have a memory location.

http://doc.rust-lang.org/reference.html#constant-items


#3

Thank you, for the link! Read the manual first, ask questions next. :smile:


#4

But, why isn’t the compiler capable of inferring which non-mutable let bindings are (composed from multiple) constant expressions?

Fx

let s = 42;
let ss = [0; s];

fails with the error: expected constant integer for repeat count, found variable.

I would expect the compiler to be able to detect such simple constant propagation on its own. Is it because of lifetimes or simply not implemented yet because it can be added later?


#5

In your example, it would be trivial; however, in many cases, figuring out at compile-time what the value of a variable (even an immutable variable) is, is equivalent to running the program.

For example:

fn f(x : u32) {
    let my_bool = false;
    let y = if my_bool {20} else {0};
    let z = if x > 0 {10} else {20};
    [...]
}

It takes a bit of computation to see what y will be, but z is impossible to know about at compile-time without knowing what x will be.

That’s why (AIUI) there is a distinction between “constants” and “immutable variables”; constants are defined so that we will always know what this will be at compile-time; variables may be harder, and we label things const to tell both the compiler and any programmers that we need it at compile-time. If you need it to be computed at compile-time, then make it a const. You can still do math with const expressions:

const X: u32 = 2;
const Y: u32 = 10;
const Z: u32 = X * Y;

Does that answer your question?


#6

It doesn’t so much answer my question as clarify when it could and couldn’t work. :slight_smile:
I’m not necessarily arguing against const. I think it’s great that we can specify an expression as ‘compile time evaluated’ and then have the compiler ensure that we don’t mix that with any runtime evaluated expressions.
What I’m not that big of a fan of is that the compiler won’t even try to infer constness on it’s own, which a lot of languages will do for you.

In my opinion

let s = 42;
let ss = [0; s];

should compile and in your example z should be compile time evaluable (is that a word? :smiley: ) as well if x is known at compile time.

Which leads me back to my original questions: Is lifetimes making it impossible to infer constness? Or is it simply not in the language (yet i hope) because it can be added post 1.0 without breaking code?


#7

To answer your questions: This has nothing to do with lifetimes, it’s a completely different thing.

It’s simply not in the language.


#8

Cool, thank you. I was wondering if lifetimes would complicate the matter, but I honestly couldn’t see how.
I’m hoping it will make it into the language at some point, but there are a lot more important features that should make it in first.