Understanding Rust Error E0013

Introduction to Error E0013

Rust error E0013 occurs when a const variable attempts to refer to a static variable. This is not allowed because const variables can only refer to other const variables.

Understanding Constants and Statics

In Rust, a const variable represents a constant value known at compile time which never changes. A static variable is a global variable whose lifetime lasts for the entire duration of the program. Declarations of const variables must include an initial value, while static variables may have a mutable or immutable reference.

Erroneous Code Example

fn main() {
    static X: i32 = 42;
    const Y: i32 = X;

In the code example above, the const variable Y attempts to refer to the static variable X, which is not valid.

Fixing Error E0013

To resolve error E0013, extract the value as a const and use it for both the static and const variables.

Corrected Code Example

fn main() {
    const A: i32 = 42;
    static X: i32 = A;
    const Y: i32 = A;

In the corrected code example, the value 42 is assigned to const variable A, which is then used for both static variable X and const variable Y.


Rust error E0013 occurs when a const variable tries to refer to a static variable. To fix this error, use another const variable to store the value and reference it from both the const and static variables. By understanding the differences between constants and statics, developers can avoid error E0013 and create more efficient Rust programs.