Skip to content

Commit

Permalink
Move example closer to explaination
Browse files Browse the repository at this point in the history
  • Loading branch information
XrXr committed Jan 24, 2022
1 parent 77387cb commit be49428
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions src/items/associated-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,22 @@ type. It is written the same as a [constant item].
Unlike [free] constants, associated constant definitions undergo
[constant evaluation] only when referenced.

```rust
struct Struct;

impl Struct {
const ID: i32 = 1;
// Definition not immediately evaluated
const PANIC: () = panic!("compile-time panic");
}

fn main() {
assert_eq!(1, Struct::ID);
// Referencing Struct::PANIC causes compilation error
// let _ = Struct::PANIC;
}
```

### Associated Constants Examples

A basic example:
Expand Down Expand Up @@ -338,24 +354,6 @@ fn main() {
}
```

[Constant evaluation] timing:

```rust
struct Struct;

impl Struct {
const ID: i32 = 1;
// Definition not immediately evaluated
const PANIC: () = panic!("compile-time panic");
}

fn main() {
assert_eq!(1, Struct::ID);
// Referencing Struct::PANIC causes compilation error
// let _ = Struct::PANIC;
}
```

[_ConstantItem_]: constant-items.md
[_Function_]: functions.md
[_MacroInvocationSemi_]: ../macros.md#macro-invocation
Expand Down

0 comments on commit be49428

Please sign in to comment.