Rust errors / Everything else
Rust compiler error · checked with rustc 1.99.0
Rust error E0133: call to unsafe function `f` is unsafe and requires unsafe function or block
What it meansUnsafe code was used outside of an unsafe block.
What rustc prints
error[E0133]: call to unsafe function `f` is unsafe and requires unsafe function or block --> main.rs:4:5 | 4 | f(); // error: call to unsafe function requires unsafe function or block | ^^^ call to unsafe function | = note: consult the function's documentation for information on how to avoid undefined behavior
Real output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.
The compiler team's explanation
Unsafe code was used outside of an unsafe block.
Erroneous code example:
unsafe fn f() { return; } // This is the unsafe code
fn main() {
f(); // error: call to unsafe function requires unsafe function or block
}
Using unsafe functionality is potentially dangerous and disallowed by safety checks. Examples:
- Dereferencing raw pointers
- Calling functions via FFI
- Calling functions marked unsafe
These safety checks can be relaxed for a section of the code by wrapping the unsafe instructions with an unsafe block. For instance:
unsafe fn f() { return; }
fn main() {
unsafe { f(); } // ok!
}
See the unsafe section of the Book for more details.
Unsafe code in functions
Unsafe code is currently accepted in unsafe functions, but that is being phased out in favor of requiring unsafe blocks here too.
unsafe fn f() { return; }
unsafe fn g() {
f(); // Is accepted, but no longer recommended
unsafe { f(); } // Recommended way to write this
}
Linting against this is controlled via the unsafe_op_in_unsafe_fn lint, which is warn by default in the 2024 edition and allow by default in earlier editions.
From rustc --explain E0133 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.
Related errors
- E0061this function takes N arguments but M were supplied
- E0412cannot find type in this scope
- E0515cannot return reference to local variable
- E0728await is only allowed inside async functions and blocks
- E0070invalid left-hand side of assignment
- E0081discriminant value `3` assigned more than once
- E0152found duplicate lang item `owned_box`
- E0264unknown external lang item: `copy`
Up next
Often hit nextE0061: this function takes N arguments but M were suppliedA related error from the same family.Read it next →Did this answer your question?