Menu Close

Rust – Constant in Rust

In this article, we are going to understand Constant in Rust with their usages in detail, along with examples.

Introduction:

In the Rust programming language, constants are immutable values that remain the same throughout the entire life of a program. Constants are useful for defining fixed values that won’t change, such as mathematical constants, physical constants, or configuration settings. Rust provides the const keyword to define constants, which allows for efficient, type-safe, and expressive code. In this article, we will discuss Rust’s const keyword in detail and provide examples to illustrate its usage.

Topics to be covered:

  • Defining Constants
  • Immutability
  • Compile-time Evaluation
  • Examples
  • Conclusion

Defining Constants

To define a constant in Rust, use the const keyword, followed by the constant’s name, its type, and its value. Constants must be assigned a value at the time of declaration, and their types must be explicitly annotated. The naming convention for constants is to use uppercase letters with underscores separating words. Here’s an example of defining a constant:

const PI: f64 = 3.141592653589793;

In this example, we define a constant named PI with a type of f64 (64-bit floating-point number) and assign it the value of π (pi).

Immutability

Constants in Rust are immutable by nature, which means they cannot be changed after they are assigned a value. This is different from variables declared with the let keyword, which can be made mutable by adding the mut keyword. Constants are valuable because they provide safety and ensure that a value remains unchanged throughout the program’s execution.

Compile-time Evaluation

Constants are evaluated at compile time, ensuring that their values are always known and fixed at the time of compilation. This allows for optimizations and improved performance. It also means that constant expressions must be composed of literals, constant functions, or other constant expressions.

Examples

Here are some examples to demonstrate the use of const in Rust:

Defining Constants:

const MAXIMUM_ATTEMPTS: u32 = 5;

const SPEED_OF_LIGHT: f64 = 299_792_458.0; // Meters per second

Using Constants:

fn calculate_circumference(radius: f64) -> f64 {

    2.0 * PI * radius

}


fn main() {

    let radius = 5.0;

    println!("Circumference of a circle with radius {} is: {}", radius, calculate_circumference(radius));

}

Constants in Array Lengths:

const ARRAY_LENGTH: usize = 10;


fn main() {

    let my_array: [i32; ARRAY_LENGTH] = [0; ARRAY_LENGTH];


    for (i, element) in my_array.iter().enumerate() {

        println!("my_array[{}] = {}", i, element);

    }

}

Conclusion:

The const keyword in Rust provides a way to define immutable, compile-time evaluated values that improve code safety and performance. Constants can be used for various purposes, such as mathematical constants, configuration settings, or array lengths. By understanding and utilizing Rust’s constants, developers can write more efficient and expressive code.

To check more Go related articles. Pls click given below link:

https://en.wikipedia.org/wiki/Rust_(programming_language)

https://www.techieindoor.com/go-conditional-statement-if-else-statement-in-go-golang/

Posted in Rust

Leave a Reply

Your email address will not be published. Required fields are marked *