RustArena

Step 5 of 6

Constants and naming

Constants

Some values are fixed for the whole program: the maximum health, the number of seconds in a minute, a tax rate. Rust has constants for these, declared with const:

const MAX_HEALTH: i32 = 100;
const SECONDS_PER_MINUTE: i32 = 60;

fn main() {
    let minutes = 3;
    println!("max health: {}", MAX_HEALTH);
    println!("{} minutes = {} seconds", minutes, minutes * SECONDS_PER_MINUTE);
}
max health: 100
3 minutes = 180 seconds

How const differs from let:

const MAX_HEALTH = 100;

fn main() {
    println!("{}", MAX_HEALTH);
}
error: missing type for `const` item
 --> src/main.rs:1:17
  |
1 | const MAX_HEALTH = 100;
  |                 ^ help: provide a type for the constant: `: i32`

Why bother? A named value explains itself (MAX_HEALTH versus a mysterious 100), and when the rule changes you edit one line instead of hunting for every 100. Unexplained numbers in code are called magic numbers, and code reviewers at most companies will ask you to name them.

Naming conventions

Rust has firm naming conventions, and the compiler warns when you break them:

Kind of name Style Examples
variables, functions snake_case max_speed, player_name
constants SCREAMING_SNAKE_CASE MAX_SPEED, TAX_RATE
types (coming later) UpperCamelCase Player, HashMap
warning: variable `playerScore` should have a snake case name
 --> src/main.rs:2:9
  |
2 |     let playerScore = 10;
  |         ^^^^^^^^^^^ help: convert the identifier to snake case: `player_score`

A warning doesn't stop the program from building, but treat warnings as things to fix: most teams make their automated checks (CI) fail on any warning. Names may contain letters, digits and _, but can't start with a digit.

Unused variables and the _ prefix

If you declare a variable and never use it, rustc warns, because that's often a bug (you computed something and forgot to use it):

warning: unused variable: `bonus`
 --> src/main.rs:3:9
  |
3 |     let bonus = 50;
  |         ^^^^^ help: if this is intentional, prefix it with an underscore: `_bonus`

Starting a name with _ tells the compiler "I know this isn't used yet", which is handy while you're still writing the code. A lone _ means "I don't need a name at all"; you'll meet it again in loops and patterns.

Your task

Work out how many seconds there are in 3 hours using two constants. Declare SECONDS_PER_MINUTE and MINUTES_PER_HOUR (both i32, both 60) at the top of the file, then print 3 hours = 10800 seconds, letting Rust do the math with hours and the constants.

Challenge 2

This program works, but it breaks Rust's naming conventions three times (the compiler warns about each). Rename maxPlayers to MAX_PLAYERS, currentPlayers to current_players and FreeSlots to free_slots, everywhere they appear. The output stays 5 of 8 slots taken, 3 free.

Challenge 3

A shop charges a fixed delivery fee on every order. Declare a top-level constant DELIVERY_FEE of type i32 with the value 499 (cents). In main, an item costs 2599 cents and the customer buys 2. Print the subtotal and the total including delivery:

Subtotal: 5198
Total: 5697

Let Rust do all the arithmetic.

Previous: Types: annotations and inference Next: Inline format arguments and {:?}