1 A mutable reference was used in a constant.
3 Erroneous code example:
7 #![feature(const_mut_refs)]
10 const OH_NO: &'static mut usize = &mut 1; // error!
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.
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
26 Remember: you cannot use a function call inside a constant or static. However,
27 you can totally use it in constant functions:
31 #![feature(const_mut_refs)]
33 const fn foo(x: usize) -> usize {
41 const FOO: usize = foo(10); // ok!