Rust

E0133 Unsafe Call Fix

You call a function marked unsafe, read through a raw pointer, or touch a mutable static from ordinary code — and the compiler stops you, even when the operation is obviously sound. That stop is the point: an unsafe operation carries safety conditions the compiler cannot check, and E0133 is the compiler refusing to let it run until someone has explicitly taken responsibility for those conditions. The repair is never to delete the unsafe marker or to silence the error — it is to write the unsafe block that states, in code, that you checked what the operation requires, or to mark your own function unsafe so the obligation lands on its callers instead.

Written November 26, 2026 · Tested with Rust 1.99.1 · 17 min read

By the end of this guide
  • The error: a call to an unsafe function
  • The error: dereferencing a raw pointer
  • The error: using a mutable static
  • The error: calling a foreign function

Written November 26, 2026

Tested with Rust 1.99.1

What this guide is

Some operations in Rust are not safe in all contexts or for all possible inputs. An unsafe fn has extra safety conditions — requirements every caller must uphold, which the compiler does not check. Dereferencing a raw pointer assumes the pointer is valid and aligned. Using a mutable static assumes no other thread is touching it at the same time. The compiler cannot verify any of those assumptions, so it will not let the operation appear in ordinary code at all. The error codes index states the rule in one line: unsafe code was used outside of an unsafe block.

E0133 is the report for that rule. It is worth separating from the borrow and type errors it appears next to, because its cause is never the operation itself: the same call, the same dereference, and the same static access all compile unchanged once they sit inside an unsafe block or an unsafe fn. What the error is asking for is a decision, recorded in the code. The Rust Reference puts the two halves of that decision plainly: an unsafe function defines a proof obligation that its callers must uphold, and an unsafe block states that the programmer has taken care of satisfying the extra safety conditions of everything inside it. E0133 fires at exactly the places where neither half has been written.

This guide reproduces the error in its five common shapes — a call to an unsafe fn, a raw-pointer dereference, a use of a mutable static, a call to a foreign (FFI) function, and a call to an unsafe method — then covers the special case of an unsafe operation inside an unsafe fn, where the wording of the error changes, and finishes with the two repairs and the operations that look unsafe but need no block at all.

Build every program in this guide with rustc --edition 2021, except the one program in the unsafe-function section that is compiled with --edition 2024 and labeled as such where it appears. In the compiler transcripts below, the file names are the bare names shown on the command line.

The error: a call to an unsafe function

The smallest form is the error index's own example shape: a function marked unsafe, called from a safe function. Defining the function is fine — the error lands on the call:

Rust
unsafe fn f() { println!("called"); }
fn main() { f(); }
Terminal
$ rustc --edition 2021 call.rs -o call.bin
error[E0133]: call to unsafe function `f` is unsafe and requires unsafe function or block
 --> call.rs:2:13
  |
2 | fn main() { f(); }
  |             ^^^ call to unsafe function
  |
  = note: consult the function's documentation for information on how to avoid undefined behavior

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0133`.

Read the header, because it names both halves of the repair: the call "requires unsafe function or block". Either the call goes inside an unsafe block, or the function containing the call becomes an unsafe fn itself. The note underneath is the part not to skip: the called function's documentation is where its safety conditions are written, and the block you are about to write is your statement that you met them.

The error: dereferencing a raw pointer

Creating a raw pointer is safe; reading through it is not. Here the pointer is created from a live local — as sound as a dereference gets — and it still fails, because soundness is exactly what the compiler cannot check for an arbitrary pointer:

Rust
fn main() { let x = 5; let p = &raw const x; println!("{}", *p); }
Terminal
$ rustc --edition 2021 deref.rs -o deref.bin
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
 --> deref.rs:1:61
  |
1 | fn main() { let x = 5; let p = &raw const x; println!("{}", *p); }
  |                                                             ^^ dereference of raw pointer
  |
  = note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0133`.

The caret sits on the dereference *p, not on the creation of p — the two halves of the program have different standing, and only one of them needs the block. The note lists the conditions the block will vouch for: the pointer must not be null, dangling, or unaligned, and the read must not violate aliasing rules.

The error: using a mutable static

A static mut is shared, mutable, global state with no synchronization, so every use of it — reading it as well as writing it — is an unsafe operation:

Rust
static mut COUNTER: i32 = 10;
fn main() { println!("{}", COUNTER); }
Terminal
$ rustc --edition 2021 static_mut.rs -o static_mut.bin
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
 --> static_mut.rs:2:28
  |
2 | fn main() { println!("{}", COUNTER); }
  |                            ^^^^^^^ use of mutable static
  |
  = note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0133`.

Note that this is a plain read, not a write, and it fails just the same. A read can race with another thread's write, so there is no read-only exemption for mutable statics. An immutable static, by contrast, needs no block at all — that contrast is at the end of this guide.

The error: calling a foreign function

Functions declared in an extern block are written in another language, outside the compiler's reach, so calling one is calling an unsafe function — the diagnostic is the same as for a Rust unsafe fn, because from the caller's side the situation is the same:

Rust
extern "C" { fn abs(input: i32) -> i32; }
fn main() { println!("{}", abs(-3)); }
Terminal
$ rustc --edition 2021 ffi.rs -o ffi.bin
error[E0133]: call to unsafe function `abs` is unsafe and requires unsafe function or block
 --> ffi.rs:2:28
  |
2 | fn main() { println!("{}", abs(-3)); }
  |                            ^^^^^^^ call to unsafe function
  |
  = note: consult the function's documentation for information on how to avoid undefined behavior

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0133`.

The error codes index lists "calling functions via FFI" as one of its three examples of unsafe functionality, alongside dereferencing raw pointers and calling functions marked unsafe. The declaration itself is not the error — declaring the signature compiles. Only the call needs the block — on edition 2021, as built here; edition 2024 separately requires the extern block itself to be written unsafe extern, which is a keyword on the declaration, not a block around the call.

The error: calling an unsafe method

Unsafe methods report the same way, with the method's full path named in the header. get_unchecked skips the bounds check that makes ordinary indexing safe, and its safety condition — the index must be in bounds — is the caller's to guarantee:

Rust
fn main() { let v = [10, 20, 30]; println!("{}", *v.get_unchecked(1)); }
Terminal
$ rustc --edition 2021 get_unchecked_err.rs -o get_unchecked_err.bin
error[E0133]: call to unsafe function `core::slice::<impl [T]>::get_unchecked` is unsafe and requires unsafe function or block
 --> get_unchecked_err.rs:1:51
  |
1 | fn main() { let v = [10, 20, 30]; println!("{}", *v.get_unchecked(1)); }
  |                                                   ^^^^^^^^^^^^^^^^^^ call to unsafe function
  |
  = note: consult the function's documentation for information on how to avoid undefined behavior

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0133`.

The shape is identical to the plain-function case — an unsafe operation outside an unsafe context — and so is the fix. What changes is only where the safety conditions are documented: on the method, in the standard library documentation, rather than on a function in your own code.

The special case: an unsafe operation inside an unsafe function

There is one context where the same call compiles on edition 2021 with no block at all: inside another unsafe fn. Historically, the body of an unsafe function counted as an unsafe block, so the obligation the caller accepted covered the operations inside too:

Rust
unsafe fn f() { println!("called"); }
unsafe fn g() { f(); }
fn main() { unsafe { g(); } }
Terminal
$ rustc --edition 2021 unsafe_fn_body.rs -o unsafe_fn_body.bin
$ ./unsafe_fn_body.bin
called

That acceptance is being phased out, in the error index's own words, "in favor of requiring unsafe blocks here too", and the phasing is controlled by the unsafe_op_in_unsafe_fn lint — allow by default in earlier editions, warn by default in the 2024 edition. The same program compiled with --edition 2024 still compiles and runs, but the call now draws a warning in the E0133 form, with the header shortened to "requires unsafe block":

Terminal
$ rustc --edition 2024 unsafe_fn_body.rs -o unsafe_fn_body24.bin
warning[E0133]: call to unsafe function `f` is unsafe and requires unsafe block
 --> unsafe_fn_body.rs:2:17
  |
2 | unsafe fn g() { f(); }
  |                 ^^^ call to unsafe function
  |
  = note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
 --> unsafe_fn_body.rs:2:1
  |
2 | unsafe fn g() { f(); }
  | ^^^^^^^^^^^^^
  = note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/unsafe-op-in-unsafe-fn.html>
  = note: `#[warn(unsafe_op_in_unsafe_fn)]` (part of `#[warn(rust_2024_compatibility)]`) on by default

warning: 1 warning emitted

For more information about this error, try `rustc --explain E0133`.
$ ./unsafe_fn_body24.bin
called

And the lint can be turned up by hand on edition 2021. With the lint denied, the identical call is a hard error — the full E0133, again with the shortened header, because inside an unsafe fn the "or function" half of the usual wording no longer applies: the function is already unsafe, and only a block is missing:

Rust
#![deny(unsafe_op_in_unsafe_fn)]
unsafe fn f() { println!("called"); }
unsafe fn g() { f(); }
fn main() { unsafe { g(); } }
Terminal
$ rustc --edition 2021 deny_lint.rs -o deny_lint.bin
error[E0133]: call to unsafe function `f` is unsafe and requires unsafe block
 --> deny_lint.rs:3:17
  |
3 | unsafe fn g() { f(); }
  |                 ^^^ call to unsafe function
  |
  = note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
 --> deny_lint.rs:3:1
  |
3 | unsafe fn g() { f(); }
  | ^^^^^^^^^^^^^
  = note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/unsafe-op-in-unsafe-fn.html>
note: the lint level is defined here
 --> deny_lint.rs:1:9
  |
1 | #![deny(unsafe_op_in_unsafe_fn)]
  |         ^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0133`.

The practical reading: code in an unsafe fn that relies on the implicit block works today on edition 2021, warns on edition 2024, and errors the moment the lint is denied. Writing the inner block now — the form the error index calls "the recommended way to write this" — is the same code in all three worlds.

The fix: wrap the operation in an unsafe block

The direct repair is the one the error index itself demonstrates: wrap the unsafe instructions in an unsafe block. For the call, nothing else changes:

Rust
unsafe fn f() { println!("called"); }
fn main() { unsafe { f(); } }
Terminal
$ rustc --edition 2021 fixed_call.rs -o fixed_call.bin
$ ./fixed_call.bin
called

The block is a statement, not a decoration. By writing it, you state that you have taken care of the called function's safety conditions — which means reading them first, in the function's documentation, as the diagnostic's note directs. Keep the block as small as the operation allows: the Book's advice is to keep unsafe blocks small and to enclose unsafe code within a safe abstraction with a safe API where one is warranted, so that the places a memory-safety mistake can hide stay few and findable.

The dereference takes the same move, with only the read inside the block and the pointer's creation left outside it, where it belongs:

Rust
fn main() { let x = 5; let p = &raw const x; unsafe { println!("{}", *p); } }
Terminal
$ rustc --edition 2021 fixed_deref.rs -o fixed_deref.bin
$ ./fixed_deref.bin
5

Because the block is a claim, the working habit is to record the reason next to it — why this particular operation satisfies its conditions, in terms a later reader can re-check. For the dereference, the reason is the pointer's provenance:

Rust
fn main() {
    let x = 5;
    let p = &raw const x;
    // SAFETY: p was created from the live local x directly above, and x is
    // neither moved nor mutated between that creation and this read.
    unsafe { println!("{}", *p); }
}
Terminal
$ rustc --edition 2021 fixed_deref_comment.rs -o fixed_deref_comment.bin
$ ./fixed_deref_comment.bin
5

The mutable static, with the mutation and the read of the result both inside the block, and the value copied out before it is printed:

Rust
static mut COUNTER: i32 = 10;
fn main() { let v = unsafe { COUNTER += 1; COUNTER }; println!("{}", v); }
Terminal
$ rustc --edition 2021 fixed_static_mut.rs -o fixed_static_mut.bin
$ ./fixed_static_mut.bin
11

The foreign call — the block vouches that the argument satisfies the foreign function's requirements, here a plain integer passed by value to C's abs:

Rust
extern "C" { fn abs(input: i32) -> i32; }
fn main() { unsafe { println!("{}", abs(-3)); } }
Terminal
$ rustc --edition 2021 fixed_ffi.rs -o fixed_ffi.bin
$ ./fixed_ffi.bin
3

And the unsafe method, where the block vouches for the index: 1 is in bounds for a three-element array, which is the condition get_unchecked leaves to the caller:

Rust
fn main() { let v = [10, 20, 30]; unsafe { println!("{}", *v.get_unchecked(1)); } }
Terminal
$ rustc --edition 2021 fixed_get_unchecked.rs -o fixed_get_unchecked.bin
$ ./fixed_get_unchecked.bin
20

The fix: make the caller an unsafe function

Sometimes the containing function cannot discharge the conditions itself, because whether they hold depends on what its own caller passes in. Then the honest repair is the other half of the diagnostic's wording: mark the containing function unsafe, so the obligation is written into its signature and lands on its callers. Here g cannot vouch for f on its own behalf, so g becomes unsafe — and, per the recommended form, still wraps the call in its own block, which discharges the conditions of f using the conditions that the callers of g have accepted:

Rust
unsafe fn f() { println!("called"); }
unsafe fn g() { unsafe { f(); } }
fn main() { unsafe { g(); } }
Terminal
$ rustc --edition 2021 caller_unsafe.rs -o caller_unsafe.bin
$ ./caller_unsafe.bin
called

The obligation has not vanished; it has moved to where it can actually be discharged. main now calls g inside a block, exactly as it would have called f — the chain ends at a caller who knows the conditions hold. Choose this fix when the safety of the operation genuinely depends on the caller's inputs. When the function can check or guarantee the conditions itself, the block fix above is the honest one, and marking the function unsafe only exports an obligation it could have discharged internally.

Where unsafe is not needed

The block is priced per operation, not per topic, and two neighbors of the error shapes compile with no unsafe anywhere. Creating raw pointers is safe code — the Book states it directly: you can create raw pointers in safe code; you just cannot dereference them outside an unsafe block:

Rust
fn main() { let mut num = 5; let _r1 = &raw const num; let _r2 = &raw mut num; num += 1; println!("{}", num); }
Terminal
$ rustc --edition 2021 create_ptr.rs -o create_ptr.bin
$ ./create_ptr.bin
6

And reading an immutable static is an ordinary safe access. Only the mut in static mut makes the static an unsafe operation, because only a mutable static can be mutated by another thread while you read it:

Rust
static LIMIT: i32 = 10;
fn main() { println!("{}", LIMIT); }
Terminal
$ rustc --edition 2021 safe_static.rs -o safe_static.bin
$ ./safe_static.bin
10

Defining an unsafe fn, declaring an extern function, and creating a raw pointer are all declarations or constructions — none of them performs the unsafe operation — on edition 2021, as built here; edition 2024 separately requires the extern block itself to be written unsafe extern, which is a keyword on the declaration, not a block around the call. E0133 attaches to the performance: the call, the dereference, the use. A sweep that wraps declarations in blocks, or marks safe functions unsafe "to be safe", misplaces the marker and trains readers to ignore it.

Which fix for which cause

Cause Message Fix
Call to an unsafe fn from a safe function E0133, "call to unsafe function … requires unsafe function or block" Wrap the call in an unsafe block, or mark the caller unsafe fn
Dereference of a raw pointer E0133, "dereference of raw pointer is unsafe…" Wrap only the dereference in an unsafe block; creating the pointer needs none
Use of a mutable static, read or write E0133, "use of mutable static is unsafe…" Wrap the use in an unsafe block
Call to a foreign function in an extern block E0133, "call to unsafe function" Wrap the call in an unsafe block; the declaration needs no block on edition 2021 (2024: the block itself is written unsafe extern)
Call to an unsafe method such as get_unchecked E0133, "call to unsafe function" Wrap the call in an unsafe block, with the method's condition met
Unsafe operation inside an unsafe fn, no inner block Compiles on 2021; warning[E0133] on 2024; error[E0133] if the lint is denied Add the inner unsafe block — valid under every edition and lint level
Creating a raw pointer, reading an immutable static, defining an unsafe fn No error Leave it — none of these performs an unsafe operation

Checklist

  • Read the first word of the diagnostic header. It names the operation — call to unsafe function, dereference of raw pointer, use of mutable static — and that tells you which safety conditions you are about to vouch for.
  • Read the called function or method's documentation before writing the block. The diagnostic's note points there for a reason: the block states that those specific conditions are met, and writing it unread makes the statement false by default.
  • Keep the block as small as the operation allows — the dereference, not the function; the call, not the surrounding logic. Small blocks are where a later memory bug can and cannot be, and that is most of their value.
  • Record why the operation is safe next to the block, in terms a reader can re-check — where the pointer came from, why the index is in bounds, why no other thread can touch the static here.
  • Do not remove unsafe from the callee, add #[allow] for the error, or wrap a whole function body to make the diagnostic stop. Each of those deletes the record of an obligation instead of discharging it.
  • Inside an unsafe fn, write the inner block anyway. The implicit block is being phased out: it warns on edition 2024 and is a hard E0133 under deny(unsafe_op_in_unsafe_fn).
  • Reach for unsafe fn on the caller only when the conditions depend on the caller's inputs. If your function can guarantee them itself, discharge them with a block and keep your function's signature safe.
  • Check you have not wrapped the safe neighbors. Pointer creation, immutable statics, and declarations need no block; wrapping them dilutes the marker for the places that do.

Sources

  • Rust Compiler Error Index, E0133 — for unsafe code being used outside of an unsafe block, for the unsafe fn f() called from main erroneous example and its unsafe { f(); } fix, for dereferencing raw pointers, calling functions via FFI, and calling functions marked unsafe as the examples of unsafe functionality, and for unsafe code in an unsafe function being phased out in favour of unsafe blocks, controlled by the unsafe_op_in_unsafe_fn lint.
  • The Rust Reference, The unsafe keyword — for an unsafe function defining extra safety conditions that all callers must uphold and the compiler does not check, for such a function being callable only from an unsafe block or an unsafe function without the lint, and for an unsafe block stating that the programmer has taken care of satisfying the extra safety conditions of the operations inside it.
  • The Rust Programming Language, Unsafe Rust — for the five unsafe superpowers, for keeping unsafe blocks small and enclosing unsafe code in a safe abstraction with a safe API, and for raw pointers being creatable in safe code while dereferencing one requires an unsafe block.

Keep Exploring