The Intricate Relationship Between let, mut, and const in Rust

This article is suitable for anyone who wants to understand Rust variable declarations—even if you haven’t figured out whether a “variable” is male or female.

Hello everyone, I am your emotional observer, Xiao R. Today, we won’t talk about love or code performance; instead, let’s discuss— the “marriage system” in Rust.

That’s right, you read it correctly. In the “law-abiding society” of Rust, every variable must be “registered in marriage,” and <span>let</span>, <span>mut</span>, and <span>const</span> are the three key terms that determine your “marital status.”

Are you ready? Let’s head to the “civil affairs bureau.”

Act One: let — Free Love, Legal Registration

In the world of Rust, all variables must be registered before they can be used. This “registration” action is done using the <span>let</span> keyword.

let name = "Xiao R";

This line of code means: “Civil affairs bureau, I want to register a variable called <span>name</span>, she is currently called ‘Xiao R’, and from now on, she will be a legal resident.”

Here’s the key point:

  • <span>let</span> is the “marriage certificate” for variables; without it, the variable is considered “unregistered.”
  • By default, Rust variables are immutable, meaning—once married, you cannot change names.

Therefore, the following code will directly result in a “divorce” (compilation error):

let name = "Xiao R";
name = "Da R"; // Compilation error: You cannot change her name!

The Rust compiler will sternly inform you: “Are you trying to commit domestic violence? Rust does not allow domestic violence. Once a variable is defined, it cannot be changed.”

Act Two: mut — Contract Marriage, Allowing Changes

But what if I want to change the name? For example, from “Xiao R” to “Da R”? It’s simple, just sign a “mutable agreement” before marriage, which means adding <span>mut</span>.

let mut name = "Xiao R";
name = "Da R"; // Legal operation, the agreement allows changes

Congratulations, you have successfully registered a “mutable variable.” <span>mut</span> is like a clause in a prenuptial agreement: “Allowed to change names, profile pictures, and statuses during the marriage.”

But please note:<span>mut</span> can only be used after <span>let</span> and cannot be used alone. It is not a variable name or a spell—it symbolizes “mutability.”

Act Three: const — Eternal Vow, Unchangeable

If <span>let</span> is marriage registration, and <span>mut</span> is a mutable agreement, then <span>const</span> is—an eternal vow.

<span>const</span> represents a “constant”; from the moment it is born, it is destined to be unchangeable and globally visible.

const PI: f64 = 3.14159;

This line of code means: “I hereby make a cosmic-level vow: <span>PI</span> will always equal 3.14159; whoever changes it is betraying mathematics.”

<span>const</span>‘s three iron rules:

  1. Must be named in uppercase (a convention in the Rust community; failure to comply will lead to ridicule)
  2. Must specify the type; cannot rely on inference
  3. Can only be used for constant expressions; cannot be the result of runtime calculations
const MAX_USERS: usize = 1000; // Legal
const NOW: u64 = std::time::SystemTime::now().duration_since_epoch().as_secs(); // Error! Can only be known at runtime!

<span>const</span> is like a “loyalty vow” in marriage; once made, it must be followed for a lifetime, even the compiler is moved.

Summary of the Three Relationships

Keyword Mutable? Scope Typical Use
<span>let</span> Default immutable Local Regular variable
<span>let mut</span> Mutable Local Variables that need modification
<span>const</span> Absolutely immutable Global Global configurations, mathematical constants

Xiao R’s Emotional Advice: How to Choose Your “Marriage Model”?

  • If you just want to store a value, use <span>let</span>, simple and safe.
  • If you need “post-marriage growth,” such as counters or state updates, please add <span>mut</span>.
  • If you vow “never to change,” such as <span>MAX_LEVEL</span> or <span>API_URL</span>, please use <span>const</span>.

Remember: in the world of Rust, there is no such dangerous operation as “default mutable”. All changes must be “legally registered” in the code, clearly stated.

Special Reminder: Rust Does Not Allow “Infidelity” in Marriage

let x = 5;
let x = x + 1; // This is called "variable shadowing"; it is not modification, but "divorce and remarry"

This line of code seems to modify <span>x</span>, but it actually “re-registers a variable with the same name”; the old <span>x</span> is discarded. This is like: “I divorced ‘Xiao x’, and now I married ‘Da x’, but the name is still x.”

Rust allows this kind of “legal remarriage” (actually replacement), but does not allow you to secretly change values.

Conclusion: Be a Responsible Programmer

Rust’s <span>let</span>, <span>mut</span>, and <span>const</span> are not just syntax; they represent a programming philosophy: clarity, safety, and no nonsense.

Just like marriage,

<span>let is a commitment, </span>

  • <span>mut is tolerance, </span>
  • <span>const is faith.</span>

So, next time you write code, don’t ask, “Can this variable be changed?” Instead, ask yourself: “Are you ready to take on this ‘responsibility’?”

Interactive Topic What was the most you used <span>mut</span> for in your project? (Share your “mutable evidence” in the comments)

#Rust #Programming #VariableDeclaration #ProgrammerEmotions #CodeMarriageStudies

This article is purely for entertainment, but the code is absolutely real. It must compile before publication.

Leave a Comment