File tree 1 file changed +22
-0
lines changed 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,28 @@ runtime-initialisation for global variables.
27
27
- Allow ` const fn ` to return types wth destructors.
28
28
- Disallow constant expressions which would result in the destructor being called (if the code were run at runtime).
29
29
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
+
30
52
# Drawbacks
31
53
[ drawbacks ] : #drawbacks
32
54
You can’t perform that action at this time.
0 commit comments