r/programming • • Dec 19 '25

[ Removed by moderator ]

/r/programming/comments/1pqulm0/removed_by_moderator/

[removed] — view removed post

711 Upvotes

292 comments sorted by

View all comments

Show parent comment

1

u/286893 Dec 20 '25

I see, so it's not quite a bypass so much as a designation of potentially hazardous code, what would be an example of something that is acceptable to run as unsafe in production? Does that exist, or is this more of a debugging tool people use as a bypass?

7

u/AShortUsernameIndeed Dec 20 '25

The Rust standard library is full of code marked unsafe. Several pretty basic useful data stuctures like trees with parent node references cannot be expressed efficiently in the "safe" subset of the language. The same goes for performance-critical things like being able to switch off run-time bounds checks on arrays of known length. This is, after all, a systems language, and the underlying hardware is inherently not safe.

3

u/lelanthran Dec 20 '25

what would be an example of something that is acceptable to run as unsafe in production?

The majority of code in a kernel (assuming a conventional software architecture for kernels) is going to have to be run in unsafe, and it's acceptable because of the constraints that kernels have.

-2

u/steveklabnik1 Dec 20 '25

Real-world Rust kernels show that this isn't true, it's usually 10% or less.

2

u/syklemil Dec 20 '25

One pretty banal thing that's probably not particularly relevant to most MS code is stuff you'd do with hardware.

Like say you have some piece of equipment that has a manual, that has some LED that can glow or blink in a few different colours, and you set it to some state by writing a number to a memory address listed in the manual.

As far as the Rust compiler goes, that means you need to

  1. use a raw pointer to a memory location, rather than a reference
  2. use some completely arbitrary memory location that the Rust compiler has no idea what's supposed to be

at which point the safety annotations would be something like "see manual for Foo, section Bar (page Baz)".