In this tutorial you will learn about the Rust Constants and its application with practical example.
Rust Constants
Constants are basically immutable literals whose values cannot be modified or changed during the execution of program. In Rust, constants live for the entire lifetime of a program and constants in Rust have no fixed address in memory.
Initializing Constants In Rust
The Constants are created in the same way you create variables but instead of using the let keyword here we use the const keyword and by convention constant names are always preferred in uppercase.
Syntax:-
1 |
const <name> = <value>; |
or
1 |
const <name> : <type> = <value>; |
Example:-
1 |
const MAXAGE : i32 = 35; |