Skip to content

Commit

Permalink
Rollup merge of #78250 - camelid:document-inline-const, r=spastorino
Browse files Browse the repository at this point in the history
Document inline-const

Part of #76001.

r? @spastorino
  • Loading branch information
jonas-schievink committed Oct 24, 2020
2 parents 77cd5b5 + 9775ac6 commit 20ba9ff
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/doc/unstable-book/src/language-features/inline-const.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# `inline_const`

The tracking issue for this feature is: [#76001]

------

This feature allows you to use inline constant expressions. For example, you can
turn this code:

```rust
# fn add_one(x: i32) -> i32 { x + 1 }
const MY_COMPUTATION: i32 = 1 + 2 * 3 / 4;

fn main() {
let x = add_one(MY_COMPUTATION);
}
```

into this code:

```rust
#![feature(inline_const)]

# fn add_one(x: i32) -> i32 { x + 1 }
fn main() {
let x = add_one(const { 1 + 2 * 3 / 4 });
}
```

You can also use inline constant expressions in patterns:

```rust
#![feature(inline_const)]

const fn one() -> i32 { 1 }

let some_int = 3;
match some_int {
const { 1 + 2 } => println!("Matched 1 + 2"),
const { one() } => println!("Matched const fn returning 1"),
_ => println!("Didn't match anything :("),
}
```

[#76001]: https://github.com/rust-lang/rust/issues/76001

0 comments on commit 20ba9ff

Please sign in to comment.