Start learning free

LESSON 11 OF 414 · VARIABLES

Variable Naming Rules in Rust: snake_case and Valid Names

Lesson Read the full explanation

TL ; DR:

  • In Rust, we use the snake_case convention to name our variables. It implies separating different words with an underscore (_), like this:

    let company_name = "Apple";
    
    let founding_year = 1976;
    
    let is_unicorn = true;
    
    let number_of_employees = 137000;
    

    The compiler even prints a non_snake_case warning when a variable name is written in camelCase.



Full lesson:

Variable names must consist ONLY of letters, numbers and underscores and they must start with a letter or underscore.

Bad variable names:

let 18spam: i32;
let x.15: String;
let #name: bool;

If you try to create a variable with any of these names, Rust will throw an error.


Naming conventions: snake_case

Sometimes a variable name might consist of multiple words, for example if you want to store the first name of a person. You might be tempted to separate those names by whitespaces, like this:

let first name = "Kate";

This code will also throw an error.

When you name your variables in Rust, you use the snake_case convention:

snake_case = separate different words with an underscore (_), like this:

let first_name = "Jane";
let last_name = "Doe";

Other languages use camelCase (separate different words with capital letters), but in Rust this style is not the convention. It still compiles, and the compiler prints a non_snake_case warning:

let firstName = "Jane";
let lastName = "Doe";

It's important to be consistent. Do not combine both styles in the same code, like this:

let first_name = "Jane";
let lastName = "Doe";

Case sensitive:

Variable names are case sensitive:

let name = "Kate";
let Name = "Andy";
let NAME = "George";

println!("{} {} {}", name, Name, NAME); // Output: Kate Andy George

Although Rust compiles this (with non_snake_case warnings for Name and NAME), it's always confusing and a bad coding practice to differentiate variable names just by case!


Mnemonic Variable Names:

Since we programmers are given a choice in how we choose our variable names, there is a bit of "best practice".

Sometimes we want them short like x and y, but sometimes we want them descriptive, especially if they are important.

Here is a great example:

let a = 25;
let b = 13;
let c = a * b;
println!("{}", c);

compared to:

let hours = 25;
let rate = 13;
let pay = hours * rate;
println!("{}", pay);

Rust understands and is happy with both of these pieces of code. But it's clear which version makes your reader happier.

Assignment
Follow the Coding Tutorial and let's name some variables well!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 3
    Ctrl + Enter to check

    RUST · VARIABLES

    Variable Naming Rules in Rust, explained

    Two kinds of rules

    Variable names in Rust follow two sets of rules that are easy to confuse. The first are hard rules: break one and the code doesn't compile. The second are conventions: break one and the code still works, but the compiler warns you, and other Rust programmers will find your code harder to read.

    The hard rules

    A name may contain letters, digits and underscores, and it must not start with a digit. It can't contain spaces or symbols such as # or .. It also can't be a keyword like match or let. Each of these fails to compile:

    let 18spam: i32 = 1;
    let first name = "Kate";
    let #name: bool = true;
    let match = 5;
    
    • A name that starts with a number is reported as expected identifier, found `18spam`, with the help text identifiers cannot start with a number.
    • A space ends the name early. In let first name = "Kate"; the compiler stops at name and complains that it expected one of :, ;, =, @ or |.
    • The # in let #name is an error too: expected pattern, found `#`.
    • A keyword gives expected identifier, found keyword `match`. The compiler suggests r#match, a raw identifier, but you are better off choosing another word.

    The convention: snake_case

    When a name has several words, Rust code writes them in lowercase and separates them with underscores. This style is called snake_case:

    let company_name = "Apple";
    let founding_year = 1976;
    let is_unicorn = true;
    let number_of_employees = 137000;
    

    Writing the same name in camelCase, with capital letters between words, is accepted by the compiler but warned about:

    let firstName = "Jane";
    println!("{}", firstName);
    

    The program compiles and runs, but the compiler prints warning: variable `firstName` should have a snake case name and suggests first_name. The warning comes from the non_snake_case check.

    Other kinds of Rust names use other styles. Constants are written in SCREAMING_SNAKE_CASE, and types in CamelCase. Variables and functions always use snake_case.

    Names are case-sensitive

    Uppercase and lowercase letters make different names, so these are three separate variables:

    let name = "Kate";
    let Name = "Andy";
    let NAME = "George";
    
    println!("{} {} {}", name, Name, NAME); // Output: Kate Andy George
    

    It compiles, and the output is Kate Andy George. But Name and NAME each trigger the snake-case warning, and telling variables apart by case alone is confusing. Pick names that differ in more than their capitals.

    Choose names that explain themselves

    Rust accepts any valid name, so the choice is yours. Compare:

    let a = 25;
    let b = 13;
    let c = a * b;
    println!("{}", c);
    

    with:

    let hours = 25;
    let rate = 13;
    let pay = hours * rate;
    println!("{}", pay);
    

    Both print 325. The second tells the reader what the numbers mean. Short names like x and y are fine for small, obvious uses, but anything that matters deserves a descriptive name. A name that starts a yes/no question, like is_employed or has_account, signals a bool.

    A name that begins with an underscore, like _unused, tells the compiler you declared the variable on purpose without using it, so it skips the unused-variable warning.

    Be consistent

    Don't mix styles in one program. A pair like first_name and lastName forces the reader to remember which style each name uses, and the compiler warns about the second one anyway. Warnings don't stop the build, which is exactly why they are easy to ignore. Treat them as the compiler pointing at something you will want to fix, and keep the output clean so that a new, real warning stands out.

    Solving the exercise

    You declare three variables with let, using snake_case for each:

    1. first_name holds "John". The two words are joined by an underscore, because a space isn't allowed.
    2. is_employed holds true. The is_ prefix makes it read as a question that has a yes or no answer.
    3. years_of_experience holds 5. Four words, all lowercase, joined with underscores.
    let first_name = "John";
    let is_employed = true;
    let years_of_experience = 5;
    

    None of the variables needs mut, since they are never changed. Printing them with println!("{} {} {}", first_name, is_employed, years_of_experience) gives John true 5.

    Key takeaways

    • Names use letters, digits and underscores, can't start with a digit, can't contain spaces or symbols, and can't be keywords.
    • Rust variables use snake_case. camelCase compiles but produces a non_snake_case warning.
    • Names are case-sensitive, but don't rely on case alone to tell variables apart.
    • A descriptive name is worth the extra typing.
    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free