Hacker News new | comments | ask | show | jobs | submit login

Minor nit pick but don't you typically do something like this in C

pid_t childPid;

switch (childPid = fork()) {

case -1: ... /error handling /;

case 0: ... /Child Specific/

default: sleep (5); }

edit - seems to mangle formatting but something like that seems fairly clean.

You're missing the point in actually a really important way.

Nobody is claiming that C makes it impossible to cleanly do the right thing—obviously the whole world runs on C.

The point is that nothing about the C language, libraries, or toolchain discourage the example given in the blogpost compared to your more correct code. Unless you remember exactly the right details from the manpages, there's nothing about the example in the blogpost that's less natural to write than your more correct code. (And people do forget those details: http://rachelbythebay.com/w/2014/08/19/fork/ )

By contrast, as illustrated in the blogpost, the most natural way to do the same thing in Rust turns out to be the more correct thing. If you wanted the bad behavior to happen, you'd have to go out of your way to pass -1 to kill(). Hence in this example, Rust's design is an improvement.

It's great that C gives you enough rope to hang yourself with, but it's even better if tying yourself to things safely is easy, and to hang yourself you have to really go out of your way.

Typically, I would hope so. A good code review process should typically catch when it isn't done like this.

In context, "typically" means your devops people get to work on Christmas to patch a critical CVE being actively exploited in the wild pissing off all your customers that one time someone didn't do the typical pattern anywhere in your codebase or the codebases of any of your 3rd party libaries, frameworks, applications...

Where "didn't do the typical pattern" might be if conditions as shown, or forgetting the "case -1", or missed a key and typed "case 1", or elided the "break;" from the case above (I note no "break;"s in your switch ;)), or didn't rtfm closely enough to see -1 was a special exit code, or mis-assumed kill(-1,...) was a noop, or ...

It's not that you can't write the code properly in C, it's that the language and function interfaces give you no handrails to protect you from a mistake. And in this case, doing the wrong thing could be disastrous to your system, especially if you had to run it as root.

Great intro to Rust and Unix post!

yes, but quite a lot of tutorials i've seen / intro to systems courses seem to think it looks "cleaner" to use an if statement; switch is definitely the move here but i've seen the if version quite a lot

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