Email me

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.

E0133Everything else
Activein rustc 1.99.0
8related errors linked below

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

All 518 Rust error codes · Rust crate data · Glossary

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?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ