E001 -- Borrow Checker Error¶
E001 comes from the ownership and borrow checker. The compiler runs it during
compilation (not during --check), and reports the violation with the rule it
broke.
Example¶
Returning a borrow from a function is not allowed:
fn f() -> &Int {
let x = 1;
return &x;
}
fn main() { }
warning[E001]: 3:3: cannot return a borrow from a function
Current behaviour¶
The compiler reports some borrow violations as warnings rather than hard
errors today, so a build can still produce a binary. Treat every E001 as an
error in your code: the diagnostic states a rule the language does not permit
(for example, references cannot be returned from a function or stored in a
struct field). --diagnostics=json reports them under borrow_warnings.
Fix¶
- Return an owned value, or take output parameters by reference
(
&mut T) and mutate through them. - For designs that need a borrow to cross a scope, use owned values, handles or indices; see the memory model guide.
Notes¶
- E001 is also the code used for borrow diagnostics in
--diagnostics=jsonoutput (kindisborrow_errororborrow_warning).