There's the UnwindSafe marker trait that isn't exactly the same thing but addresses the only real case I've seen for forbidding panics which is FFI.
A lot of panics are placed instead of proper error handling as a cause of happy-path programming. (E.g. all cases of .unwrap())
Some panics are placed not knowing they’re panics. (E.g. indexing, calling a function that panics)
Some panics are legitimately placed by people who want a program to panic at a given place; this may collide with the caller’s desire to use Result and error types.
There are lots of situations where being notified about where the panics live can make your code more robust, because they do sneak in.
https://doc.rust-lang.org/std/macro.unreachable.html
In those cases you strike a deal with the devil. The type system won't help you.
Maybe it's an acceptable tradeoff for readability and efficiency.
Maybe you could have modelled the code so it wasn't necessary.
I think you can divide the use of `unreachable!()` in two cases:
1) As a consequence of style (applying LEM to code)
2) As a consequence of low-level abstractions
One can be avoided by remodelling. The other can be tucked away behind safe interfaces.I'm not saying that lint should be enabled by default. It's a highly specialized config, but it's an important protection when it's useful.
Otherwise, like duped said, using get will return an Option<T>, and so you'd either have to use unreachable!() and panic anyway, or you'd have to propagate the error up the call stack and let it affect all the types of those functions (at which point, what is the caller supposed to do about it?).
Or you can use get_unchecked which is unsafe and avoids the bounds check. If you pass the bounds check up higher then you have no risk of panic.
My point is that forbidding code from panicking isn't that useful - you still need to audit the code to make sure it's correct.
And this still won't solve the problem of when you as a programmer know that something is definitely inbound but you can't express the proof in the language. Ultimately this means using `panic` (but you don't want to), dummy default values (but this is just `null` all over again!), adding dependent types (which AFAIK nobody has managed to do in a low level language, and even if it was possible it would add a lot of complexity) or just give up and accept your program cannot be accepted.
https://doc.rust-lang.org/std/panic/fn.catch_unwind.html#not...
And they are common only in two scenarios: if you set panic="abort", or special UB checks that the standard library does. The former is just a simple configuration change, the latter is not really something you can handle in any way.
There are probably some reasons Rust is only non-C language to get into Linux kernel.
Strictly speaking you could obviously say reference counting is a form of GC. So to avoid GC you then surely recommend people to use C since that, in contrast to Rust, Swift, C++ and many others, really doesn’t use any GC, neither managed nor reference counting based.
In Rust, reference counting is opt-in for individual objects. It is a necessity for any powerful, low-level language to be able to implement any high-level feature.
You can, after all, also implement reference counting in C.
To avoid GC, you need a language where it’s opt-in. For example C++ or Zig, but not OCaml.
I assume LWN or some other Linux adjacent news outlet should have written something about it.
Also trying to get government funding for a particular language seems like a lobbying to me.
- an addition to the critical infrastructure information technology sector,
- a cloud computing tax to fund critical U.S. cyber defense
- U.S.-sponsored governance for emerging cybersecurity solutions like Rust, and
- a U.S.-sponsored open source library verification service.
Some relevant quotes:
- Cloud sales tax:
-- "A cloud computing tax is long overdue, and it must be collected to secure the software supply chain for American consumers."
-- "A cloud sales tax would put the cost of securing open source for U.S. economic stability on the companies that have profited the most from open source software—its biggest consumers. The Open Source Trust can offer financial support to open source communities, allow for more free-flowing exploration of our technology frontier, and close a gaping hole in America’s economic stability."
- "A public-private partnership effort to build an actionable cookbook for memory-safety migration would be a better first step than urging technology manufacturers to use the one available today." ... "CISA should partner with early Rust adopters to identify their insights, costs, and wins and visibly incorporate that data into the roadmap guidance." ... "CISA should lead an initiative to create this cookbook for memory-safety migration starting with Rust, where there is little institutional knowledge available today, and this work should be funded by the Open Source Trust."
- Because Rust's memory safety and analysis tools are limited, and because engineers "need education and tools to know when to use [unsafe Rust] and how to mitigate the risks 'unsafe Rust' introduces," CISA SEI should "receive Open Source Trust funding to continue their research and development and (a) reduce the limitations of the Rust compiler, (b) audit the Rust compiler’s correctness in assessing the memory safety of Rust code, and (c) develop both static and dynamic analysis tools for safe and unsafe Rust."
- Also, CISA should "receive additional Open Source Trust funding to support rapid, in-depth development of standards across package repositories, compilers, and build tools" to mitigate the the security problems that come from one person controlling a crate that thousands depend on.
This isn't that important, but it's interesting, because I have often heard complaints here that Rust is hard to read.
"Rust is also the easiest programming language to sight-read. Engineers reading new code are like musicians reading unfamiliar sheet music. There are always recognizable elements, but the theme, pace, and key may be outside of the player’s experience. In software, those unfamiliar elements can take a developer through a complicated maze of dependencies and logic trees, and Rust makes the trail of logic in a program easier to follow. Researchers have concluded that Rust has a significantly lower cognitive complexity than C, C++, Python, JavaScript, and TypeScript (all languages studied), “meaning that [Rust] can guarantee the highest understandability of source code compared to all others.” As a result, software maintainers can understand unfamiliar Rust code far more quickly than code wri0en in many other popular languages."
They cite this study: https://www.ncbi.nlm.nih.gov/pmc/articles/PMC7959618/
I like Rust, but this sounds like opposite world. I have trouble imagining how this conclusion was made.
Writing the signatures can become a pain because you have to think everything through up front, but we spend more time reading code than writing it.