Skip to content

Commit 4096dd6

Browse files
committed
Add more examples, get everything passing at last.
1 parent 3f0ca55 commit 4096dd6

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

src/doc/reference.md

+33-6
Original file line numberDiff line numberDiff line change
@@ -1271,10 +1271,12 @@ 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. (See below on [static lifetime
1275-
elision](#static-lifetime-elision).) The compiler is, however, still at
1276-
liberty to translate the constant many times, so the address referred to may not
1277-
be stable.
1274+
address will have elided lifetimes where applicable, otherwise – in most cases –
1275+
defaulting to the `static` lifetime. (See below on [static lifetime elision].)
1276+
The compiler is, however, still at liberty to translate the constant many times,
1277+
so the address referred to may not be stable.
1278+
1279+
[static lifetime elision]: #static-lifetime-elision
12781280

12791281
Constants must be explicitly typed. The type may be `bool`, `char`, a number, or
12801282
a type derived from those primitive types. The derived types are references with
@@ -1298,6 +1300,8 @@ const BITS_N_STRINGS: BitsNStrings<'static> = BitsNStrings {
12981300
};
12991301
```
13001302

1303+
1304+
13011305
### Static items
13021306

13031307
A *static item* is similar to a *constant*, except that it represents a precise
@@ -1364,7 +1368,7 @@ such, the constant declarations involving `'static` above may be written
13641368
without the lifetimes. Returning to our previous example:
13651369

13661370
```rust
1367-
#[feature(static_in_const)]
1371+
# #![feature(static_in_const)]
13681372
const BIT1: u32 = 1 << 0;
13691373
const BIT2: u32 = 1 << 1;
13701374

@@ -1382,6 +1386,27 @@ const BITS_N_STRINGS: BitsNStrings = BitsNStrings {
13821386
};
13831387
```
13841388

1389+
Note that if the `static` or `const` items include function or closure
1390+
references, which themselves include references, the compiler will first try the
1391+
standard elision rules ([see discussion in the nomicon][elision-nomicon]). If it
1392+
is unable to resolve the lifetimes by its usual rules, it will default to using
1393+
the `'static` lifetime. By way of example:
1394+
1395+
[elision-nomicon]: https://doc.rust-lang.org/nomicon/lifetime-elision.html
1396+
1397+
```rust,ignore
1398+
// Resolved as `fn<'a>(&'a str) -> &'a str`.
1399+
const RESOLVED_SINGLE: fn(&str) -> &str = ..
1400+
1401+
// Resolved as `Fn<'a, 'b, 'c>(&'a Foo, &'b Bar, &'c Baz) -> usize`.
1402+
const RESOLVED_MULTIPLE: Fn(&Foo, &Bar, &Baz) -> usize = ..
1403+
1404+
// There is insufficient information to bound the return reference lifetime
1405+
// relative to the argument lifetimes, so the signature is resolved as
1406+
// `Fn(&'static Foo, &'static Bar) -> &'static Baz`.
1407+
const RESOLVED_STATIC: Fn(&Foo, &Bar) -> &Baz = ..
1408+
```
1409+
13851410
### Traits
13861411

13871412
A _trait_ describes an abstract interface that types can
@@ -2079,7 +2104,9 @@ macro scope.
20792104

20802105
### Miscellaneous attributes
20812106

2082-
- `deprecated` - mark the item as deprecated; the full attribute is `#[deprecated(since = "crate version", note = "...")`, where both arguments are optional.
2107+
- `deprecated` - mark the item as deprecated; the full attribute is
2108+
`#[deprecated(since = "crate version", note = "...")`, where both arguments
2109+
are optional.
20832110
- `export_name` - on statics and functions, this determines the name of the
20842111
exported symbol.
20852112
- `link_section` - on statics and functions, this specifies the section of the

0 commit comments

Comments
 (0)