Why do we need constants in Rust if we have immutable variables?
Asked Answered
L

3

7

So I'm exploring Rust, and I have read about technical differences between constants and immutable variables. But it seems like immutable variables can do all things that constants can. Then what is the point of existence of constants, if immutable variables can fully substitute them?

Lornalorne answered 18/1, 2022 at 17:55 Comment(4)
Does this answer your question? What is the difference between immutable and const variables in Rust?Batik
@Batik they only show the difference, but it doesn't make it clear why we need constants at all. Immutable variables can do same things and even more.Lornalorne
A valuable comment about memory: https://mcmap.net/q/158593/-what-is-the-difference-between-immutable-and-const-variables-in-rustHills
hah I was asking what the point of immutable variables are when const exists! But yes the same question, I wish the doc.rust-lang.org/book/… covered this better.Koroseal
B
4

There are two computational times that you should take into account:

  • compilation time
  • run time

The constant is computed at compilation time (and can be used in other compile-time computation) and hence the run time is faster, as it does need to compute it again.

Immutable variables are always computed at run time (from an external input not available at compilation time usually), and constants cannot be used there.

Brougham answered 18/1, 2022 at 18:12 Comment(0)
M
1

Then what is the point of existence of constants, if immutable variables can fully substitute them?

While there are certainly use cases in which constants may be interchangeable with immutable variables, the main distinction between the categories of values is their semantics.

  • Declaring a constant immediately says a lot about what the value is to the reader: in particular, that the information that comprises the value must all be available at compile-time. This is a property which is enforced by the compiler. This sets up expectations for the reader about what the value is and what can be done with it.

  • Of course, the initialization of immutable variables is much more flexible. There is no mandate that these values are known at compile time, and the calculations that produce such values may be arbitrarily complex and even evolve over time.

The differences are, perhaps, mainly stylistic (in many but not all use cases) but where readability and maintainability are involved the distinction is valuable.

Muntjac answered 18/1, 2022 at 18:39 Comment(0)
A
1
  • const x can never be changed. Useful because all constants could be declared in a central place (not distributed over the entire code).
  • let x (without mut) can be moved (move semantics, i.e. the owner can change). Besides it is possible to (re-)declare it later in the code with another let or let mut (which overwrites the old value of the “immutable” variable).

Therefore a const has to be used for an array size, because it has to be known for sure at compile time.

Example 1:

const HELLO: &str = "Hello, I feel so solid";
println!("{}", HELLO); // Hello, I fee…
let new_hello= HELLO; // does not move to new_hello, but
                      // new_hello is a copy of the const
println!("{}", new_hello); // Hello, I fee…
println!("{}", HELLO); // still exists

let bye = "See you later".to_string();
println!("{}", bye); // See you…
let new_bye = bye;
println!("{}", new_bye); // See you…
//println!("{}", bye);  // not possible, because content                                                                            
                        // of bye has moved to new_bye
                        // (which is the new owner)

Example 2:

let x = 2; // an immutable variable without `mut`                                                                                   
           // attribute
println!("{x}"); // 2

let x = 10; // we declare a new variable with the same
            // name x with a new value
            // Note: `let x = …` is possible, 
            //       but `x = …` is not!
println!("{x}"); // 10

let mut x = 1000; // again declaration with same name,
                  // but mutable
println!("{x}"); // 1000
x = -5;        // Note: `x = …` is possible 
               //       because of `let mut`
println!("{x}"); // -5 (`x` is mutable now)
Appall answered 29/3, 2023 at 23:13 Comment(1)
this should be the accepted answerRaama

© 2022 - 2024 — McMap. All rights reserved.