Rust | (Solved) “Missing type for `const` or `static`”

Have you come across the following error error: missing type for const item in Rust? Chances are, you defined a constant value like this:

const MY_CONSTANT_VALUE = 1; // this will generate errors at compile time
static ANOTHER_VALUE = 1;  // this will generate errors at compile time

At first, this doesn’t seem to be an error as Rust is capable of implicitly determine the data type of other non-constant scoped variables (let ) like the one you see in the following example:

fn main() {
   let my_value = 1; // Rust will implicitly determine the data type of my_value as i32
}

To fix error: missing type for const item you must explicitly define the data type of a constant and static value in Rust.

Constants must be explicitly typed.

Rust Documentation: Constant items

Hence, to solve the previous error from the previous snippet of code, we explicitly define the constant variable MY_CONSTANT_VALUE with the data type i32.

const MY_CONSTANT_VALUE: i32 = 1;
static ANOTHER_VALUE: i32 = 1;

Did this article help you fix the error?

Share your thoughts by sending a message on Twitter of Become A Better Programmer or to my Twitter account!