Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I have to specify the type for "const" variables but not for "let" variables?

To create a variable in Rust you'd use:

let var_name = 10;

This would also be valid:

let var_name: i32 = 10;

Constant variables are created like this:

const VAR_NAME: i32 = 10;

But if you tried to create a constant variable like this:

const VAR_NAME = 10;

You'd get an error that looks like this:

error: expected `:`, found `=`
 --> src/main.rs:5:11
  |
4 |   const VAR_NAME = 10;
  |                 ^ expected `:`

Having come from languages like JavaScript, Python & PHP this is kind of confusing to me.

Why is it that I have to specify a type definition when using const but not when I use let?

like image 520
LogicalBranch Avatar asked Apr 05 '19 08:04

LogicalBranch


People also ask

Does it matter if I use const or let?

As a general rule, you should always declare variables with const, if you realize that the value of the variable needs to change, go back and change it to let. Use let when you know that the value of a variable will change. Use const for every other variable.

Why define constants at all if you can use regular variables instead of them?

Constants are used when you want to assign a value that doesn't change. This is helpful because if you try to change this, you will receive an error. It is also great for readability of the code. A person who reads your code will now know that this particular value will never change.

What is the main difference between the const and let keywords in variable declaration?

var declarations are globally scoped or function scoped while let and const are block scoped. var variables can be updated and re-declared within its scope; let variables can be updated but not re-declared; const variables can neither be updated nor re-declared. They are all hoisted to the top of their scope.


1 Answers

Currently, there is a rule "Constants must be explicitly typed." (for static: "A static item is similar to a constant").

But, you are right: the compiler could infer it. There is an open discussion about that: #1349, TL;DR:

  • We could technically infer the type of const and static variable
  • We do not use them very often so it's not very annoying to annotate the types
  • We should maybe limit type inference for constants/statics to only literal value
  • This could make error messages less accurate
  • Maybe limit type inference for constants/statics to local scopes like function bodies
  • For integers, const FOO = 22 would infer to i32 so probably not the type one would expect. So we'd end up writing const FOO = 22usize.
  • When the variable is initialized with a const-fn, the type should be inferred
  • When the variable is initialized with another variable typed explicitly
  • For arrays, the explicit type is very redundant
  • For variables which are only exported, we can end up not being able to infer their types (so it would be a compile-time error "type needs to be specified")

It may be worth mentioning that one of the guiding principle of type inference in Rust is that type inference should be local. This is the reason why, unlike in Haskell, function signatures always need to be fully specified. There are multiple reasons for this, notably it means easier reasoning for human readers and better error messages. This puts module level const in a tough spot, inference-wise. Matthieu M.

So far, there is still not a proposed RFC, so this issue stays open.

See also:

  • Why does Rust not permit type inference for local constants?
like image 125
Stargateur Avatar answered Oct 14 '22 14:10

Stargateur