constants
Constants store data that cannot be modified, just like immutable variables.
The key differences are:
- They are defined at compile time.
- They are defined in the global scope.
- Use the keyword
const
instead oflet
. - By convention, named in SCREAMING_SNAKE_CASE.
- Their type must be annotated.
- Currently they only can store literal constants
use debug::PrintTrait;
const ONE_HOUR_IN_SECONDS: u32 = 3600;
const TICKTACKTOE_PLAYER_COUNT: u8 = 2;
const IS_COMPLETE: bool = 1;
fn main() {
ONE_HOUR_IN_SECONDS.print();
}
Try it out!
- Install the toolchain:
- For macOS and Linux, run our script:
curl -sL https://raw.githubusercontent.com/lambdaclass/cairo-by-example/main/build/installer.sh | bash -s 2.2.0
- For Windows and others, please see the official guide
- Run the example:
- Copy the example into a constants.cairo file and run with:
%!s(<nil>) constants.cairo