Constant in Rust

In this article, we have explained the use and properties of Constant in Rust and how it is different from a mutable and immutable variable in Rust.

Table of contents:

  1. Constant in Rust
  2. Constant Array in Rust
  3. Constant vs Variable in Rust

Let us get started with constant in Rust.

Constant in Rust

Constant in Rust is a immutable variable that is the value cannot be changed once defined. It cannot be shadowed like variables. It is defined using const keyword.

The syntax of defining a constant in Rust is:

const CONSTANT_NAME:data_type = constant_value;

A constant in Rust can be defined as:

// PIE value in Rust
const PIE:f32 = 3.14159;

In the above code statement:

  • const is the keyword in Rust to define a constant.
  • PIE is the constant name.
  • f32 is the data type that is floating point 32 bits.
  • 3.14 is the value of the constant.

The complete Rust code example is as follows:

fn main() {
   // PIE value in Rust
   const PIE:f32 = 3.14159;
   println!("Pie value is: {}", PIE);
}

The commands to run the above Rust code saved as code.rs file are:

rustc code.rs
code.exe

Output:

Pie value is: 3.14159

A string in constant is defined as:

const COMPANY:&str = "opengenus";

Properties of Constant in Rust

The Properties of Constant in Rust are:

  • Constant is always mutable in Rust.
  • Constant cannot be shadowed (like variables) in Rust.
  • Constant is defined using const keyword.
  • Constant can be declared in any scope within a Rust program.
  • Constant can be defined only for constant expression and cannot be the result of an expression that is calculated at run time.
  • Constant is valid for the entire duration of program execution within the scope it was defined.

Constant should be used in define values that do not change like value of pie and other attributes for the program. It makes the overall program readable.

Constant Array in Rust

A constant array in Rust is defined as follows:

const VALUES: [i32; 5] = [11, 9, 10, -5, 99];

// or

const VALUES: &'static [i32] = &[11, 9, 10, -5, 99];

Note:

  • i32 is for Integer 32 bit constant.
  • &'static [T] is pointer indirection

A constant array with string in Rust is defined as follows:

const COMPANY: &'static [&'static str] = &["google", "opengenus", "facebook"];

A multi-dimensional array of size 128 x 128 can be defined as follows:

const 2D_ARRAY: [[i32; 128]; 128] = [[-99, 100, …], […], …];

Constant vs Variable in Rust

The differences between constant and variable in Rust are:

  • Variables are immutable by default but can be made mutable by using "mul" keyword while defining a variable. On the other hand, constant is always immutable and cannot be made mutable.

  • Variables are defined using "let" keyword while constants are defined using "const" keyword.

let pie_variable = 3.14159;
const pie_constant::f32 = 3.14159;
  • mul keyword is used to make a variable mutable. mul keyword can be used only with variable and cannot be used with constant.
// mutable variable
let mul pie_variable = 3.14159;

// this will give an error
const mul pie_constant::f32 = 3.14159;
  • Variables can be shadowed while Constant cannot be shadowed.

In this code, variable COMPANY is

let COMPANY = "opengenus";
let COMPANY = COMPANY.len();

The above code with variable work. Note that the variable is immutable by default.

The same code with constant give an error:

const COMPANY:&str = "opengenus";
// Error
const COMPANY:usize = COMPANY.len();

With this article at OpenGenus, you must have a strong idea of using constant in Rust. Enjoy.