diff options
| author | Jonas Schievink <jonasschievink@gmail.com> | 2020-10-24 14:12:08 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-24 14:12:08 +0200 |
| commit | 20ba9ffbeeb72e83b82026a71c061b9e858a0b82 (patch) | |
| tree | 3a50962f0faa990a16c596cbe0e703b57df61137 /src/doc | |
| parent | 77cd5b5485a9ba13ff291cc993cff44cf81e1901 (diff) | |
| parent | 9775ac60dcc92dbb04861863a69eacfafeea5c4c (diff) | |
| download | rust-20ba9ffbeeb72e83b82026a71c061b9e858a0b82.tar.gz rust-20ba9ffbeeb72e83b82026a71c061b9e858a0b82.zip | |
Rollup merge of #78250 - camelid:document-inline-const, r=spastorino
Document inline-const Part of #76001. r? @spastorino
Diffstat (limited to 'src/doc')
| -rw-r--r-- | src/doc/unstable-book/src/language-features/inline-const.md | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/doc/unstable-book/src/language-features/inline-const.md b/src/doc/unstable-book/src/language-features/inline-const.md new file mode 100644 index 00000000000..00e1c79ca3f --- /dev/null +++ b/src/doc/unstable-book/src/language-features/inline-const.md @@ -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 |
