loading...

re: My first impressions of Rust VIEW POST

FULL DISCUSSION
 

I don't see the point of having the const keyword when let is immutable by default. It seems more like syntax sugar for the old school constants concept.

If my memory's correct, const is to declare a "compile-time constant expression", i.e. a constant expression that can entirely be resolved during compile time.

That means that the expression will be resolved during compilation time, and results used in place of the expression right after.

 

How is it different from having a top level immutable let statement in terms of usage? May be I'm missing something obvious

 

let is kept at runtime, so you'd need to access the value inside the variable every time.

Every const expression access is replaced at compile time by its value, which means that instead of accessing a variable's content, it's like you hard-coded the value.

let a = 5;
const b = 10;

fn main() {
    println!("{}", a);
    println!("{}", b);
}

would produce

let a = 5;

fn main() {
    println!("{}", a);
    println!("{}", 10);
}

Thanks, that makes more sense. I'll update the post accordingly

code of conduct - report abuse