Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Rust Variable Names


Share

Variable Names

In the Variables chapter, you gave your variables names like name and age.

A good name tells you what the value is for. When you read age, you know what the variable holds, without looking at its value.

Rust has a few rules for names. If you break them, the program does not compile. Rust also has a naming style that all Rust programmers follow.


Naming Rules

  • Names can contain letters, digits and underscores (_)
  • Names must begin with a letter or an underscore, not a digit
  • Names cannot contain spaces or special characters like !, - or #
  • Names are case-sensitive (age and Age are two different variables)
  • Reserved words (Rust keywords, like let, fn and if) cannot be used as names

Example

let age = 25;
let first_name = "John";
let player2 = "Liam";

println!("{}", age);
println!("{}", first_name);
println!("{}", player2);
Try it Yourself »

Names That Do Not Work

A name that starts with a digit gives an error, because Rust thinks it is a number:

Example

let 2nd_place = "Liam"; // Error
println!("{}", 2nd_place);
Try it Yourself »

To fix it, start the name with a letter instead, for example second_place.

The same goes for keywords. Rust uses words like let, fn, if, else, match, loop, while, for, mut and const for its own code, so they cannot be variable names.



Use snake_case

When a name has more than one word, Rust programmers write all letters in lowercase and put an underscore between the words. This style is called snake_case:

StyleExampleUsed in Rust for
snake_casefirst_name, total_scoreVariables and functions
UPPER_SNAKE_CASEMAX_SPEEDConstants
UpperCamelCasePerson, DirectionStructs and enums (you will learn about them later)

If you write a variable name in another style, like firstName, the program still runs. But when you build it on your own computer, the Rust compiler shows a warning: variable `firstName` should have a snake case name. It even suggests the name first_name.


Choose Clear Names

Short names like x are fine in small examples, but in a real program, use names that describe the value:

Example

let minutes_per_hour = 60;
let hours = 3;
let total_minutes = minutes_per_hour * hours;

println!("{} hours is {} minutes", hours, total_minutes);
Try it Yourself »

The same program with the names a, b and c would give the same result, but it would be much harder to understand what it does.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.