Skip to content

Commit 437df54

Browse files
committed
Document cfg(version)
1 parent 8db4edd commit 437df54

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/conditional-compilation.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ evaluates to true or false. The predicate is one of the following:
4141
if at least one predicate is true. If there are no predicates, it is false.
4242
* `not()` with a configuration predicate. It is true if its predicate is false
4343
and false if its predicate is true.
44+
* `version()` with a version number inside. It is true if the language version
45+
the compiler targets is higher or equal to the contained version number.
46+
It is false otherwise.
4447

4548
_Configuration options_ are names and key-value pairs that are either set or
4649
unset. Names are written as a single identifier such as, for example, `unix`.
@@ -205,6 +208,22 @@ production. For example, it controls the behavior of the standard library's
205208
Set when the crate being compiled is being compiled with the `proc_macro`
206209
[crate type].
207210

211+
## The `version()` predicate
212+
213+
The `version()` predicate evaluates to true if both:
214+
215+
* The version number contained inside follows the format and
216+
* The version number contained inside is less than or equal to the version
217+
of the language the compiler targets. Usually the compiler version and
218+
language version match. So compiler version `1.50.0` targets language
219+
`1.50.0`.
220+
221+
In order for it to be considered of valid format, the version number has to
222+
follow either the `"a.b.c"` scheme or the `"a.b"` scheme, where `a,b,c` are
223+
decimal integers between `0` and `65535`, inclusively. Semantically, assume `c`
224+
to be 0 if not present. Order wise, version numbers behave as if they were
225+
Rust tuples `(a,b,c)` with `a,b,c` being `u16` integers.
226+
208227
## Forms of conditional compilation
209228

210229
### The `cfg` attribute
@@ -250,6 +269,12 @@ fn on_32bit_unix() {
250269
fn needs_not_foo() {
251270
// ...
252271
}
272+
273+
// This function is only included if the language version is newer than 1.50.0
274+
#[cfg(version("1.50.0"))]
275+
fn needs_new_compiler() {
276+
// ...
277+
}
253278
```
254279

255280
The `cfg` attribute is allowed anywhere attributes are allowed.

0 commit comments

Comments
 (0)