Skip to content

Commit 074fd55

Browse files
committed
this is actually stable
so it's fine to keep it in
1 parent e575f9e commit 074fd55

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/items.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,52 @@ unsafe fn bump_levels_unsafe2() -> u32 {
663663
Mutable statics have the same restrictions as normal statics, except that the
664664
type of the value is not required to ascribe to `Sync`.
665665

666+
#### `'static` lifetime elision
667+
668+
[Unstable] Both constant and static declarations of reference types have
669+
*implicit* `'static` lifetimes unless an explicit lifetime is specified. As
670+
such, the constant declarations involving `'static` above may be written
671+
without the lifetimes. Returning to our previous example:
672+
673+
```rust
674+
const BIT1: u32 = 1 << 0;
675+
const BIT2: u32 = 1 << 1;
676+
677+
const BITS: [u32; 2] = [BIT1, BIT2];
678+
const STRING: &str = "bitstring";
679+
680+
struct BitsNStrings<'a> {
681+
mybits: [u32; 2],
682+
mystring: &'a str,
683+
}
684+
685+
const BITS_N_STRINGS: BitsNStrings = BitsNStrings {
686+
mybits: BITS,
687+
mystring: STRING,
688+
};
689+
```
690+
691+
Note that if the `static` or `const` items include function or closure
692+
references, which themselves include references, the compiler will first try the
693+
standard elision rules ([see discussion in the nomicon][elision-nomicon]). If it
694+
is unable to resolve the lifetimes by its usual rules, it will default to using
695+
the `'static` lifetime. By way of example:
696+
697+
[elision-nomicon]: ../nomicon/lifetime-elision.html
698+
699+
```rust,ignore
700+
// Resolved as `fn<'a>(&'a str) -> &'a str`.
701+
const RESOLVED_SINGLE: fn(&str) -> &str = ..
702+
703+
// Resolved as `Fn<'a, 'b, 'c>(&'a Foo, &'b Bar, &'c Baz) -> usize`.
704+
const RESOLVED_MULTIPLE: Fn(&Foo, &Bar, &Baz) -> usize = ..
705+
706+
// There is insufficient information to bound the return reference lifetime
707+
// relative to the argument lifetimes, so the signature is resolved as
708+
// `Fn(&'static Foo, &'static Bar) -> &'static Baz`.
709+
const RESOLVED_STATIC: Fn(&Foo, &Bar) -> &Baz = ..
710+
```
711+
666712
### Traits
667713

668714
A _trait_ describes an abstract interface that types can

0 commit comments

Comments
 (0)