Skip to content

Commit

Permalink
Merge pull request rust-lang#677 from matthewjasper/const_constructor
Browse files Browse the repository at this point in the history
Document `const_constructor` feature
  • Loading branch information
Centril authored Oct 27, 2019
2 parents beb79db + f7af030 commit 4b21b64
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/items/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,13 @@ aborts the process by executing an illegal instruction.

## Const functions

Functions qualified with the `const` keyword are const functions. _Const
functions_ can be called from within [const context]s. When called from a const
context, the function is interpreted by the compiler at compile time. The
interpretation happens in the environment of the compilation target and not the
host. So `usize` is `32` bits if you are compiling against a `32` bit system,
irrelevant of whether you are building on a `64` bit or a `32` bit system.
Functions qualified with the `const` keyword are const functions, as are
[tuple struct] and [tuple variant] constructors. _Const functions_ can be
called from within [const context]s. When called from a const context, the
function is interpreted by the compiler at compile time. The interpretation
happens in the environment of the compilation target and not the host. So
`usize` is `32` bits if you are compiling against a `32` bit system, irrelevant
of whether you are building on a `64` bit or a `32` bit system.

If a const function is called outside a [const context], it is indistinguishable
from any other function. You can freely do anything with a const function that
Expand Down Expand Up @@ -214,7 +215,9 @@ Exhaustive list of permitted structures in const functions:
are all permitted.

This rule also applies to type parameters of impl blocks that
contain const methods
contain const methods.

This does not apply to tuple struct and tuple variant constructors.

* Arithmetic and comparison operators on integers
* All boolean operators except for `&&` and `||` which are banned since
Expand Down Expand Up @@ -389,6 +392,8 @@ fn foo_oof(#[some_inert_attribute] arg: u8) {
[_WhereClause_]: generics.md#where-clauses
[_OuterAttribute_]: ../attributes.md
[const context]: ../const_eval.md#const-context
[tuple struct]: structs.md
[tuple variant]: enumerations.md
[external block]: external-blocks.md
[path]: ../paths.md
[block]: ../expressions/block-expr.md
Expand Down

0 comments on commit 4b21b64

Please sign in to comment.