E0133: call to unsafe function requires unsafe block Rust Origin: Application & API OSI layer 7 · Application Last updated 5 Nov 2026

Rust: error[E0133]: call to unsafe function `foo` is unsafe and requires unsafe function or block

You called an unsafe function (or dereferenced a raw pointer / accessed a static mut) outside unsafe.

Exact message you may see error[E0133]: call to unsafe function `foo` is unsafe and requires unsafe function or blockdereference of raw pointer is unsafe and requires unsafe blockuse of mutable static is unsafe and requires unsafe function or blockE0133 call to unsafe function requires unsafe blockerror[E0133]: use of mutable static
Seen on: Rust

Meaning

Wrap in unsafe { ... } only after verifying the safety contract in the docs, or use a safe wrapper. For static mut prefer AtomicUsize, Mutex, OnceLock. In edition 2024 unsafe_op_in_unsafe_fn requires unsafe blocks inside unsafe fns too.

Common causes

  • FFI function call without unsafe block
  • Raw pointer dereference
  • static mut access
  • Calling get_unchecked, transmute, etc.
  • Edition 2024 requiring blocks inside unsafe fns

⚡ Quick fix

  1. Wrap the call in unsafe { } with a // SAFETY: comment
  2. Replace static mut with atomics/Mutex
  3. Use safe abstractions (bindgen wrappers, std APIs)

Detailed fix by platform

Rust

  1. rust
    // SAFETY: ptr is valid and aligned per the caller contract
    unsafe { *ptr }

How to diagnose

  1. Function — Unsafe contract?
  2. Alternative — Safe API?
  3. static mut — Replace?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

Blocking waiting for file lock on package cache / build directory — Cargo: Blocking waiting for file lock on package cache / on build directory (Rust)Blocking waiting for …cargo: failed to get from registry (spurious network error) — Cargo: warning: spurious network error (2 tries remaining): [35] SSL connect error / error: failed to download from `https://index.crates.io/...` (Rust)cargo: failed to get …cargo: failed to select a version for the requirement — Cargo: error: failed to select a version for the requirement `serde = "^2.0"` — candidate versions found which didn't match: 1.0.197 (Rust)cargo: failed to sele…E0015: cannot call non-const function in constants — Rust: error[E0015]: cannot call non-const function `String::from` in constants (Rust)E0015: cannot call no…E0658: use of unstable library feature — Rust: error[E0658]: use of unstable library feature `let_chains` / `#![feature]` may not be used on the stable release channel (Rust)E0658: use of unstabl…error: linker `cc` not found — Rust: error: linker `cc` not found — note: No such file or directory (os error 2) (Rust)error: linker `cc` no…feature edition2024 is required / Cargo.lock version too new — Cargo: error: failed to parse manifest — feature `edition2024` is required / lock file version 4 requires `-Znext-lockfile-bump` (Rust)feature edition2024 i…E0133: ca…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.