Does Rust formally distinguish constructs that trade compile-time checks for runtime checks?

Viewed 74

There are a number of tricks I've learned about for "getting around" Rust's restrictions without using unsafe. For example:

  • Option::unwrap
  • RefCell

There are probably others I'm forgetting.

In cases like these, responsibility for specific aspects of correctness is shifted from the compiler to the programmer. Things that would have been compilation errors become panics, and the programmer is expected to just "know that their logic is right".

Panics are better than memory corruption, but given Rust's branding as a fully-safe language, I would think these "trap-doors" would be formally identified somehow - in the type system, documentation, or otherwise - for easy identification. The programmer should know when they're using a shortcut and taking on added responsibility.

Does this kind of distinction exist? Even just an explicit list somewhere in the documentation? Is my mental model wrong, making such a thing unnecessary?

1 Answers

No, there is no formal distinction.

I believe you are asking if there is an effect system. While this has been talked about by compiler developers for a while, there is no consensus about if it would truly be beneficial or detrimental in the long run.

"getting around" Rust's restrictions

These "get around" nothing. The methods themselves ensure the requirements are upheld.

shifted from the compiler to the programmer

I disagree with this assessment. Responsibility has been shifted from compile time to run time, but the compiler and the library code still ensures that safety is upheld.

using unsafe

Unsafe code truly moves the responsibility to the programmer. However, then that programmer builds safe abstractions that other programmers can make use of. Ideally, they build the abstractions using tools that are checked at compile time, helping to reduce runtime errors.

Rust's branding as a fully-safe language

responsibility for specific aspects of correctness

Yes, Rust intends to be a memory-safe language, which does not mean that code written in Rust is correct. The branding emphasizes memory safety; other people assume that means things like "cannot crash", but we cannot prevent all mistaken interpretations.

See also:

Related