Start learning free

LESSON 13 OF 414 · VARIABLES

Constants in Rust: Declaring const Values

Lesson Read the full explanation

There is a special type of variable in Rust, which is called constant.

A constant is a variable that we do not want to change the value of during the whole program.


Creation:

You create a constant variable just like you create a normal one, but starting with the keyword const instead of let. A constant's name is written in SCREAMING_SNAKE_CASE, and its type annotation is required:

const OUR_NAME: &str = "AlgoCademy";

println!("{}", OUR_NAME); // Output: AlgoCademy

Reassignment not allowed:

However, a const variable cannot be reassigned because it is constant. If you try to reassign a const variable, you'll get an error.

For example, this code:

const OUR_NAME: &str = "AlgoCademy";

OUR_NAME = "Andy"; // Here is the problem

would produce error[E0070]: invalid left-hand side of assignment


Must be initialized:

Constant variables must be initialized when declared. If you try to declare a const variable without a value, you'll get an error.

For example, this code:

const OUR_NAME: &str;

would produce error: free constant item without body


Usage:

It seems like constants are just variables with some limitations. So, why use them when we can do the same things (and more) with normal variables?

The power of constants lies in their limitation! Constants were invented to provide some level of guarantee that the code can't change the underlying value.

This is not of much importance for a smaller project, but matters on a larger project with multiple components written by multiple authors.

Picture this: You are the first developer to have ever worked at a new startup. When you have written the core code, you created some important variables that describe your company:

const COMPANY_NAME: &str = "Apple";
const FOUNDING_DATE: &str = "April 1st, 1976";
const FOUNDER_NAME: &str = "Steve Jobs";

Ten years later, your company blew up and you have hundreds of Software Engineers managing millions of lines of code.

I bet you wouldn't love it if a Junior Developer was able to do this successfully:

COMPANY_NAME = "Microsoft";
FOUNDER_NAME = "Bill Gates";

But you are smart! You made those variables constants ten years ago and have nothing to worry about now. As for the Junior, they'll just get an error and probably a slap on the wrist by their manager.


Assignment
Follow the Coding Tutorial and let's work with constants!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    RUST · VARIABLES

    Constants in Rust, explained

    What a constant is

    A constant is a named value that can never change. Rust variables are already immutable by default, so what makes const different? A constant is fixed at compile time and can never be made mutable. A plain let variable could later be rewritten as let mut by someone editing the code, but a const can't. When a value is a fact of your program, such as a mathematical constant or a limit, const says so in the code itself.

    Declaring a constant

    You write the keyword const, then the name, then a colon and the type, then = and the value:

    const OUR_NAME: &str = "AlgoCademy";
    
    println!("{}", OUR_NAME); // Output: AlgoCademy
    

    Two details differ from a let variable:

    • The type is required. A constant never relies on inference. const LIMIT = 5; is rejected with missing type for `const` item, and the compiler suggests : i32.
    • The name is written in SCREAMING_SNAKE_CASE. That means capital letters with underscores between words, like MAX_SPEED. A lowercase constant still compiles, but the compiler warns that it should have an upper case name.

    A constant can't be reassigned

    The whole point is that nobody can change it. Assigning to a constant is an error:

    const PI: f64 = 3.14;
    
    PI = 5.0;
    
    println!("{}", PI);
    

    The compiler stops with error[E0070]: invalid left-hand side of assignment, pointing at PI with the note cannot assign to this expression. Nothing runs. Adding mut doesn't help either: const mut X: i32 = 1; fails with const globals cannot be mutable.

    A constant must have a value

    A constant can't be declared first and filled in later. const OUR_NAME: &str; is rejected with free constant item without body, and the compiler suggests providing a definition with = <expr>;.

    The value also has to be computable while the program is being compiled. Plain literals and arithmetic on other constants are fine:

    const SECONDS_PER_DAY: u32 = 60 * 60 * 24;
    println!("{}", SECONDS_PER_DAY); // Output: 86400
    

    Calling an ordinary function is not, so const NAME: String = String::from("x"); fails with cannot call non-const associated function. Use a &str constant for text.

    Why use constants

    For a tiny program, they don't matter much. In a large project with many authors, they act as a guarantee. Picture the core values that describe a company:

    const COMPANY_NAME: &str = "Apple";
    const FOUNDING_DATE: &str = "April 1st, 1976";
    const FOUNDER_NAME: &str = "Steve Jobs";
    

    Years later, when hundreds of people work on the code, a careless COMPANY_NAME = "Other"; in some distant file won't compile. The mistake is caught immediately, rather than turning up as a wrong value in production.

    Constants also remove "magic numbers". A line like if retries > 3 makes the reader wonder what 3 means, and the same number may be repeated in many places. A named constant, MAX_RETRIES, is described once and changed in one place.

    A let variable can't borrow the name of a constant either. After const PI: f64 = 3.14;, a line such as let mut PI = 2.0; fails with let bindings cannot shadow constants. This closes another back door: the name keeps its meaning everywhere it is visible.

    const against let

    letconst
    Can be made changeableyes, with mutnever
    Type annotationoptionalrequired
    Value known at compile timenot neededrequired
    Can be declared outside any functionnoyes
    Naming stylesnake_caseSCREAMING_SNAKE_CASE

    A constant declared at the top of a file, outside every function, is visible to all the code in that file. Constants can also be declared inside a function, as the exercises do.

    Solving the exercise

    The starting code declares PI and then tries to change it:

    const PI: f64 = 3.14;
    
    PI = 5.0;
    
    println!("{}", PI);
    

    This can't compile, because of the assignment. The task is to make the program print 3.14, and the way to do that is to delete the assignment line. Keep the declaration and the println! as they are:

    const PI: f64 = 3.14;
    
    
    
    println!("{}", PI);
    

    The program prints 3.14. The blank lines left behind are harmless.

    Key takeaways

    • const NAME: Type = value; declares a value that never changes.
    • The type is always required, and the name is conventionally in capital letters.
    • Assigning to a constant is a compile error, and mut is not allowed on one.
    • A constant needs a value at compile time, so it can't call ordinary functions.
    • Use constants for fixed facts and limits, and give them names instead of repeating raw numbers.
    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free