docs/reference/src/documentation/language/variables/const.md
Constants are similar to immutable let variables; however, there are a few differences:
impl scope.mut keyword cannot be used with constants.To define a constant the const keyword is used followed by a name and an assignment of a value.
{{#include ../../../code/language/variables/src/lib.sw:constants}}
The example above hardcodes the value of 5 however function calls may also be used alongside built-in types.
impl self ConstantsConstants can also be declared inside impl blocks. In this case, the constant is referred to as an associated constant.
struct Point {
x: u64,
y: u64,
}
impl Point {
const ZERO: Point = Point { x: 0, y: 0 };
}
fn main() -> u64 {
Point::ZERO.x
}