Skip to content

Commit ba60af3

Browse files
committed
Document RFC 1623: static lifetime elision.
1 parent 7766b50 commit ba60af3

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/doc/reference.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,15 +1271,16 @@ guaranteed to refer to the same memory address.
12711271

12721272
Constant values must not have destructors, and otherwise permit most forms of
12731273
data. Constants may refer to the address of other constants, in which case the
1274-
address will have the `static` lifetime. The compiler is, however, still at
1274+
address will have the `static` lifetime. (See below on [static lifetime
1275+
elision](#static-lifetime-elision).) The compiler is, however, still at
12751276
liberty to translate the constant many times, so the address referred to may not
12761277
be stable.
12771278

12781279
Constants must be explicitly typed. The type may be `bool`, `char`, a number, or
12791280
a type derived from those primitive types. The derived types are references with
12801281
the `static` lifetime, fixed-size arrays, tuples, enum variants, and structs.
12811282

1282-
```
1283+
```rust
12831284
const BIT1: u32 = 1 << 0;
12841285
const BIT2: u32 = 1 << 1;
12851286

@@ -1331,7 +1332,7 @@ running in the same process.
13311332
Mutable statics are still very useful, however. They can be used with C
13321333
libraries and can also be bound from C libraries (in an `extern` block).
13331334

1334-
```
1335+
```rust
13351336
# fn atomic_add(_: &mut u32, _: u32) -> u32 { 2 }
13361337

13371338
static mut LEVELS: u32 = 0;
@@ -1355,6 +1356,31 @@ unsafe fn bump_levels_unsafe2() -> u32 {
13551356
Mutable statics have the same restrictions as normal statics, except that the
13561357
type of the value is not required to ascribe to `Sync`.
13571358

1359+
#### `'static` lifetime elision
1360+
1361+
Both constant and static declarations of reference types have *implicit*
1362+
`'static` lifetimes unless an explicit lifetime is specified. As such, the
1363+
constant declarations involving `'static` above may be written without the
1364+
lifetimes. Returning to our previous example:
1365+
1366+
```rust
1367+
const BIT1: u32 = 1 << 0;
1368+
const BIT2: u32 = 1 << 1;
1369+
1370+
const BITS: [u32; 2] = [BIT1, BIT2];
1371+
const STRING: &str = "bitstring";
1372+
1373+
struct BitsNStrings<'a> {
1374+
mybits: [u32; 2],
1375+
mystring: &'a str,
1376+
}
1377+
1378+
const BITS_N_STRINGS: BitsNStrings = BitsNStrings {
1379+
mybits: BITS,
1380+
mystring: STRING,
1381+
};
1382+
```
1383+
13581384
### Traits
13591385

13601386
A _trait_ describes an abstract interface that types can
@@ -2458,9 +2484,6 @@ The currently implemented features of the reference compiler are:
24582484
into a Rust program. This capability, especially the signature for the
24592485
annotated function, is subject to change.
24602486

2461-
* `static_in_const` - Enables lifetime elision with a `'static` default for
2462-
`const` and `static` item declarations.
2463-
24642487
* `thread_local` - The usage of the `#[thread_local]` attribute is experimental
24652488
and should be seen as unstable. This attribute is used to
24662489
declare a `static` as being unique per-thread leveraging

0 commit comments

Comments
 (0)