Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It would be nice for beginners, since unsafe is hard. Like really hard.


Rust unsafe has pretty much the same functionality as plain C, with more vervose syntax. So I would expect this model to refuse to give any examples of C code whatsoever.


C is a much simpler language, so it's far easier to reason about the semantics of "unsafe" code.

For example: Rust has destructors (Drop trait) that run automatically when a value goes out of scope or is overwritten. If memory for a struct with a Drop trait is manually allocated (and initialized with garbage) and it is assigned to, the `drop()` method will run for the previous value of it which will cause undefined behavior.

That's just one feature: Rust also has references, tagged unions, virtual method calls (&dyn Trait), move semantics, `Pin<T>`, closures, async/await, and many more, all of which make it harder to reason about safety without the guardrails provided by the language for regular, "safe" code—for which barring a compiler bug it is actually _impossible_ to shoot yourself in the foot like this.

This is actually why it's so incredibly hard to write C++ code that is provably correct: It has even more features that could cause problems than Rust, and is _always_ in "unsafe" mode, with no guardrails.


gcc C has destructors (cleanup attr), nested functions/closures

you can do tagged unions with cpp tricks, and so-on

(sadly I have seen most of these used...)

regardless, what makes C hard is undefined behaviour


Not entirely true. You can't bypass the borrow checker for example, and you have to maintain Rust invariants when you use it. Hence the name.


To be fair, C is basically one giant footgun…


Yawn. This is such a tired trope.


Is it not true, though? Is there some other language that has a list of banned (commonly used) functions[0] in a major project?

[0] https://github.com/git/git/blob/master/banned.h


I programmed in C++ a while back - I was given constant warnings about how pointers were dangerous and to treat them with respect. I followed the Qt habits and primarily used references for everything - but there were problems I encountered that required[1] pointers and I went to those same people warning me about pointers and asked them how to use them - I expressed my hesitancy to do so and they calmly explained best practices and specific pitfalls. My code was good[2] and functioned safely - it was good and functioned safely because my knowledge sources, along with the warnings, were willing to share the dangerous knowledge. Refusing to educate people about dangerous things just means they'll wing it and be wonderful examples of why those things are dangerous.

If I asked Gemini about magic quotes in PHP[3] I'm fine with it explaining why that's a terrible idea - but it should also show me the safest path to utilize them.

1. Languages are turing complete - nothing is an absolute - etc... so "required" is a really strong word here and inaccurate - there were alternatives but they were much less elegant and clean.

2. -ish... I was a pretty junior developer at the time.

3. https://en.wikipedia.org/wiki/Magic_quotes


Imo it should give you the answer, and make you aware of the risks.

Some people won't follow the advice, but that's what code review and static analysis tools are for.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: