Chapter 9: Rust Constants

Rust Constants. This is a perfect follow-up because constants are like the “super-immutable” cousins of the let variables we talked about last time.

In Rust, constants are not the same as just making a variable immutable with let. They have special rules, special powers, and special use cases. Let me explain everything slowly and clearly, like we’re sitting together in a café near Gachibowli, with code examples you can run right now, comparisons to everyday things, and tables to make it crystal clear.

1. What Are Constants in Rust? (The Big Picture)

Constants are values that are:

  • Known completely at compile time (the compiler calculates them before your program even runs)
  • Impossible to change — ever — during the entire life of your program
  • Usually written in ALL_CAPS_WITH_UNDERSCORES (convention, like in many languages)
  • Used for fixed, unchanging values like math constants, configuration limits, magic numbers, API keys (in safe way), etc.

Rust actually has two kinds of “constants”:

  • const → the normal, most common one (this is what people usually mean by “Rust constants”)
  • static → a special global constant (or mutable global in unsafe code) with a fixed memory address

Today we’ll focus mostly on const (90% of cases), and I’ll explain static at the end so you don’t get confused.

2. Declaring a const — Syntax & Rules

Rust

Key rules you must follow (compiler will error otherwise):

  1. Type annotation required — no let-style inference
  2. Value must be computable at compile time — only simple math, no function calls (unless const fn), no runtime I/O, no Vec::new()
  3. Always immutable — no mut allowed (compiler error if you try)
  4. Cannot be shadowed — you can’t redeclare const MAX_USERS later
  5. Naming convention: SCREAMING_SNAKE_CASE (all uppercase + underscores)

3. const vs Immutable let — Why Two Things?

This is where beginners get confused — here’s the clear comparison (2026 style):

Feature let (immutable) const
Keyword let const
Type annotation Optional (inferred) Required
Value computed when? At runtime (when code executes) At compile time
Can change value? No (unless shadowed) Never — impossible
Can shadow? Yes (redeclare same name) No
Can be mutable? Yes — add mut No — forbidden
Memory location Normal variable (stack/heap) Inlined wherever used (no single location)
Can use in array sizes? No (needs const context) Yes
Global scope? Possible but rare Very common & encouraged
Typical use Normal program logic variables Fixed limits, math constants, configs

Example showing difference:

Rust

4. Real-Life Examples You Can Run

Create a quick project:

Bash

Put this in src/main.rs:

Rust

Run cargo run — see how everything is baked in at compile time.

5. Advanced but Useful: const in Array Lengths & Generics

Rust

This is impossible with normal let (even immutable) because array size must be known at compile time.

6. Quick Note on static (The Other “Constant”)

static is like const but:

  • Has a fixed memory address (single instance in the program)
  • Has ‘static lifetime
  • Can be mutable (but only in unsafe code — dangerous!)
  • Used for global state, FFI, singletons, etc.
Rust

Rule of thumb in 2026: Use const for 95% of cases. Use static only when you need a fixed address (rare for beginners).

Teacher Summary

Rust Constants (const) = values that are:

  • Fixed forever
  • Computed at compile time
  • Typed explicitly
  • Inlined (no single memory spot)
  • Perfect for magic numbers, limits, math constants, configs

They are stronger than immutable let — more like “this value is part of the program’s DNA, it never changes.”

Practice: Go add 5–10 constants to your existing projects (max score, pi value, pin codes, etc.). Try using them in array sizes or calculations — feel how clean it is!

Next topic ready?

  • static in more detail?
  • Strings (&str vs String — constants love &str)?
  • Or ownership with constants/variables?

Just say — your Hyderabad Rust teacher is right here! 🦀🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *