1- Mutable references (` &mut ` ) can only be used in constant functions, not statics
2- or constants. This limitation exists to prevent the creation of constants that
3- have a mutable reference in their final value. If you had a constant of `&mut
4- i32` type, you could modify the value through that reference, making the
5- constant essentially mutable. While there could be a more fine-grained scheme
6- in the future that allows mutable references if they are not "leaked" to the
7- final value, a more conservative approach was chosen for now. ` const fn ` do not
8- have this problem, as the borrow checker will prevent the ` const fn ` from
9- returning new mutable references.
1+ A mutable reference was used in a constant.
102
113Erroneous code example:
124
@@ -19,6 +11,18 @@ fn main() {
1911}
2012```
2113
14+ Mutable references (` &mut ` ) can only be used in constant functions, not statics
15+ or constants. This limitation exists to prevent the creation of constants that
16+ have a mutable reference in their final value. If you had a constant of
17+ ` &mut i32 ` type, you could modify the value through that reference, making the
18+ constant essentially mutable.
19+
20+ While there could be a more fine-grained scheme in the future that allows
21+ mutable references if they are not "leaked" to the final value, a more
22+ conservative approach was chosen for now. ` const fn ` do not have this problem,
23+ as the borrow checker will prevent the ` const fn ` from returning new mutable
24+ references.
25+
2226Remember: you cannot use a function call inside a constant or static. However,
2327you can totally use it in constant functions:
2428
0 commit comments