Compile-time Execution (Scoped Effect)

Description

The const keyword marks functions as "is allowed to be evaluated during compilation". When used in scope position its meaning changes slightly to: "this will be evaluated during compilation". There is no way to declare "must be evaluated at compilation" functions, causing the meaning of "const" to be context-dependent.

declarationusage
keyword never appliesfn meow() {}fn hello() { meow() }
keyword always applies-const CAT: () = {};
keyword conditionally appliesconst fn meow() {}const fn hello() { meow() }

Feature Status

The const feature is integrated in a lot of the stdlib and ecosystem already, but it's notoriously missing any form of const-traits. Because a lot of Rust's language features make use of traits, this means const contexts have no access to iteration, Drop handlers, closures, and more.

Feature categorization

PositionSyntax
Effectconst fn
YieldN/A
Applyautomatic
Consumeconst {}, const X: Ty = {}
ReificationN/A

Positions Available

PositionAvailableExample
Manual trait implN/A
Free functionsconst fn meow() {}
Inherent functionsimpl Cat { const fn meow() {} }
Trait methodstrait Cat { const fn meow() {} }
Trait declarationsconst trait Cat {}
Block scopefn meow() { const {} }
Argument qualifiersfn meow(cat: impl const Cat) {}
Data typesconst struct Cat {}
Dropimpl const Drop for Cat {}
Closuresconst ǀǀ {}
Iteratorsfor cat in cats {}

Refinements

There are currently no refiments to the compile-time execution effect.

Interactions with other effects

Asynchrony

Compile-time Execution

Fallibility

Iteration

Unwinding

Memory-Safety

Immovability

Object-Safety

Ownership

Thread-Safety

References