Skip to content

Commit a778e87

Browse files
committed
Add some examples
1 parent afea13f commit a778e87

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

text/0000-drop-types-in-const.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,28 @@ runtime-initialisation for global variables.
2727
- Allow `const fn` to return types wth destructors.
2828
- Disallow constant expressions which would result in the destructor being called (if the code were run at runtime).
2929

30+
## Examples
31+
Assuming that `RwLock` and `Vec` have `const fn new` methods, the following example is possible and avoids runtime validity checks.
32+
33+
```rust
34+
/// Logging output handler
35+
trait LogHandler: Send + Sync {
36+
// ...
37+
}
38+
/// List of registered logging handlers
39+
static S_LOGGERS: RwLock<Vec< Box<LogHandler> >> = RwLock::new( Vec::new() );
40+
```
41+
42+
Disallowed code
43+
```rust
44+
static VAL: usize = (Vec::<u8>::new(), 0).1; // The `Vec` would be dropped
45+
const EMPTY_BYTE_VEC: Vec<u8> = Vec::new(); // `const` items can't have destructors
46+
47+
const fn sample(_v: Vec<u8>) -> usize {
48+
0 // Discards the input vector, dropping it
49+
}
50+
```
51+
3052
# Drawbacks
3153
[drawbacks]: #drawbacks
3254

0 commit comments

Comments
 (0)