Rust Variables

A variable is a name given to a particular area in memory, that stores a value, which can later be reused. Variable names in Rust can contain letters, numbers and underscores, but must begin with either a letter or underscore. Variable names are also case-sensitive. It is possible to declare a variable with or without specifying the type of data that it can store, such as string, integer or floating point number. Where no data type is specified, Rust allocates a type based on the data that is assigned to it.

/// Variable without a data type specified.
let variable_name = variable_value;

/// Variable with a data type specified.
let variable_name:variable_type = variable_value;

Where a type is specified, the variable doesn't need to be assigned a value when it is declared.

// Variable with a data type specified.
let variable_name:variable_type;

// Assign a value separately, after the variable has been declared.
variable_name = variable_value;

Below are some example variables, the last of which is a Boolean variable, that can either be ‘true’ or ‘false’.

// Integer variables.
let example_integer1 = 5;
let example_integer2:i8 = 10;

// Floating point variables.
let example_float1 = 5.1;
let example_float2:f32 = 10.3;

// String variables.
let example_string1 = "This is a string";
let example_string2:String = "This is another string".to_string();

// Boolean variables.
let example_boolean1 = true;
let example_boolean2:bool = false;

There are a few things to note with these variables. Firstly, the integer data type 'i8', refers to a signed integer of 8 bits in size, which can hold both negative and positive numbers. Other options include, 'i16', 'i32', 'i64', and 'i128', for 16, 32, 64, and 128 bit signed integers. There are also unsigned equivalents, 'u8', 'u16', 'u32', 'u64', and 'u128'.

For floating point variables, 'f32' refers to a 32-bit signed floating point number. The other option is 'f64', which specifies a 64-bit floating point number.

One further thing to note is that variable names must not be the same as reserved words in Rust. Reserved words have a specific meaning and cannot be used for anything else. They are shown in blue on the preceding and following pages.

By default, variables in Rust are immutable meaning that once a value has been assigned, they are read-only and cannot be changed. If it is necessary for the value of a variable to be altered, the 'mut' reserved word needs to be placed prior to the variable name when it is declared.

let mut example_integer1 = 5;

The values of variables can be displayed, for example, in a console or terminal window. The curly brackets are used as a placeholder for the value of the variable specified after the comma.


This will display the following in the console or terminal.

This is a string
This is another string

More text can be included within the double quotes so that the variable value forms part of a larger string.

println!("Value of example_integer1: {}",example_integer1);
println!("Value of example_integer2: {}",example_integer2);
println!("Value of example_float1: {}",example_float1);
println!("Value of example_float2: {}",example_float2);
println!("Value of example_string1: {}",example_string1);
println!("Value of example_string2: {}",example_string2);
println!("Value of example_boolean1: {}",example_boolean1);
println!("Value of example_boolean2: {}",example_boolean2);

The output produced is shown below.

Value of example_integer1: 5
Value of example_integer2: 10
Value of example_float1: 5.1
Value of example_float2: 10.3
Value of example_string1: This is a string
Value of example_string2: This is another string
Value of example_boolean1: true
Value of example_boolean2: false

Multiple variables can be incorporated into a string by using more than one set of curly brackets and comma separating the variables.

println!("Value of both integers: {}, {}", example_integer1, example_integer2);

This displays as follows.

Value of both integers: 5, 10