compiler/rustc_error_codes/src/error_codes/E0764.md MARKDOWN 38 lines View on github.com → Search inside
1A mutable reference was used in a constant.23Erroneous code example:45```compile_fail,E07646fn main() {7    const OH_NO: &'static mut usize = &mut 1; // error!8}9```1011Mutable references (`&mut`) can only be used in constant functions, not statics12or constants. This limitation exists to prevent the creation of constants that13have a mutable reference in their final value. If you had a constant of14`&mut i32` type, you could modify the value through that reference, making the15constant essentially mutable.1617While there could be a more fine-grained scheme in the future that allows18mutable references if they are not "leaked" to the final value, a more19conservative approach was chosen for now. `const fn` do not have this problem,20as the borrow checker will prevent the `const fn` from returning new mutable21references.2223Remember: you cannot use a function call inside a constant or static. However,24you can totally use it in constant functions:2526```27const fn foo(x: usize) -> usize {28    let mut y = 1;29    let z = &mut y;30    *z += x;31    y32}3334fn main() {35    const FOO: usize = foo(10); // ok!36}37```

Findings

✓ No findings reported for this file.

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.