Why are we still need const in rust?

Isn't const default in rust by let PI=3.14.

Are you trying to ask a question about any one of these features?

If so, which one in particular?

Could you perhaps try to explain in a bit more detail what alternative language design you would have expected instead? Then maybe we can understand your point and/or maybe point out some (more or less) fundamental differences to let statements :wink:

6 Likes

with

const PI=3.14;
{
 PI*d
}

we have :
mul register 3.14
instead of

{
let PI=3.14;
PI*d
}

mov `register 3.14` mul register .
Beside, we don't need copy PI=3.14 copy it over and over. PI can use outside of function... because

The only lifetime allowed in a constant is 'static

That's not true.

2 Likes

Sr Can I have a question about static ?

Blockquote they represent a location in memory

It's mean const is static because it's #inline( replace value before compile to binary) and effect entire program by default.
But in static case, It alway in memory even after compiled to binary

nah but You are right. Thanks for correct
. const don't have lifetime.

The error was happened because let a: 'static = &'a "whatever";
It's doesn't have same life time

It's not clear if you mean that for your example with const or your other example, but yes, that's one of the reasons const is very useful, either at the crate or module level or inside a block or trait: the value is shared within a scope without having to be copied over and over again (which would be error-prone and not very useful).

The code using a constant is also much clearer in its intention.

1 Like

To add, you have wrong understanding about how code is compiled into final program. In practice, with optimizations enabled, both programs will compile to exact same code (both in Rust, and equivalent code in C). You shouldn't think of having a local variable as "moving data to one more register", because that's not true.

Example: Compiler Explorer

2 Likes

Indeed, as stated here, constants are simply inlined, so that makes no difference. You'll find the rules about lifetime on that page, too.

That's why they are preferred over statics, unless one of the following are true: [sic]

  • Large amounts of data are being stored.
  • The single-address property of statics is required.
  • Interior mutability is required.
1 Like

thank you.

It's clear. Thank you

1 Like

I don't know if this was mentioned already: consts are protected from shadowing in the same scope, you cannot by accident re-assign the same const, with let you can:


fn main(){
    const PI: f64 = 3.14;
    let Pi2 = 3;
    
    const PI: &str = "Pi";  //error
    let Pi2 = "abc"; //will work!
}

the error message will be:

error[E0428]: the name PI is defined multiple times

4 Likes

Also, this:

pub fn main() {
   let t = 1;
   let v = [0; t];
}

throws

error[E0435]: attempt to use a non-constant value in a constant
 src/main.rs:5:16
  |
5 |    let v = [0; t];
  |                ^ non-constant value
  |
help: consider using `const` instead of `let`
2 Likes

this’s really good feature. It help avoid naming accidents

It’s good too.only constant value was approved. when t always have owner changing ability. It helps avoid error appearing like a magic.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.