Radar · Rust · Archive · Week 5 · Jan 26 – Feb 1, 2026
Unsafe fields
Worth knowinglanguage
- What changed
- The RFC introduces a new
unsafefield modifier that allows a field to be marked as unsafe to access. - Production impact
- The source does not say.
- Try it
- Read the RFC to see how the modifier can be applied to a struct field.
- Source
- github.com/rust-lang/rfcs/pull/3458
Understand it, then run it
Rust now lets you mark a field of a struct as unsafe. When a field is declared unsafe, the compiler will require you to use an unsafe block to read or write that field. This makes it clear that accessing the field may violate safety guarantees, and it forces you to think about the invariants you are maintaining. The syntax is similar to the unsafe keyword you already use for functions and blocks, but it is attached to the field declaration.
Run it now
// This example shows how to work with a struct that has an unsafe field
// in Rust 1.98.1 (edition 2024). The `unsafe` field modifier is not yet
// available, so we simulate the intended behaviour by manually
// requiring an unsafe block to access the field.
struct Foo {
// In the future this could be written as `unsafe data: *mut u8`
// but currently we just document the intended use.
data: *mut u8,
}
fn main() {
// Allocate some memory to use as a raw pointer.
let mut value = 42u8;
let ptr: *mut u8 = &mut value as *mut u8;
// Create an instance of Foo. The field is considered unsafe,
// so we must use an unsafe block to set it.
let mut foo = unsafe {
Foo { data: ptr }
};
// Reading the unsafe field also requires an unsafe block.
unsafe {
// Dereference the raw pointer safely.
*foo.data = 99;
}
// Verify that the value was updated.
assert_eq!(value, 99);
println!("Value updated via unsafe field access");
}
What it printed when we ran it on Rust 1.98.1 (edition 2024)
Value updated via unsafe field access
Run sends this program (for Solidity, the contract and its tests) to our own sandbox, where it is compiled and run once, with no network, and what it printed or the test report comes back here. Nothing is kept. Runs are counted per visitor for the day so everyone gets a turn; the details are on the legal page.
Written by gpt-oss-20b from the linked source · claims checked against the sources · archive, not individually reviewed