The point here is that the language's tools and APIs can significantly better drive the developer towards the safe/right solution, that's a large point of type theory and static type systems after all. In this case rust's type system is used to split out the various "result cases" and notify the developer upfront of the various cases to handle. The return type pretty much tells you how the function will behave and what you need to take care of as the caller.
That aside, why would you unwrap_or(default_value) if you're in a hurry when unwrap() is shorter (and you can later grep for "unwrap()" to find dodgy/hurried code, whereas unwrap_or is a perfectly legitimate recovery strategy).
> Now, if you're about to tell me that the examples above are totally stupid and no developer would do such a thing, then you know how I feel about the sloppy C versions. Doing a system call and not checking for error is totally stupid as well.
The issue being that even though you have a compiled statically typed language it's of absolutely no help in "checking for error", and interactions between syscalls can be hard to predict, not checking for fork(2)'s error isn't the end of the world... until you pass its result to kill(2) for instance (it might also give strange results if you pass specific pids to waidpid)
The evidence is on our side that well-design language significantly reduces number of defects in production code if other variables are equal.
By contrast, strong static typing does in fact prove the absence of certain errors.
Sure, you can have the same higher level API in C. Though Rust will not compile if there's no exhaustive match, and you cannot access the `int ret` if it's in the wrong state, but it's close enough.
struct fork_status {
enum { ERROR, PARENT, CHILD } state;
int ret;
};
I think the point of tagged unions (as in OP's example) is that you cannot access `child` if the call failed. Your example does have an exhaustive check (by a compiler warning), but it doesn't prevent you from misinterpreting `ret` as a pid. Does C have a method to enforce this restriction?pid_t isn't a newtype, it's just a typedef, so that doesn't really make a difference.
> System calls should really return two values, return value and errno, but C doesn't handle that very well either.
Most system calls want to return either, not both. And C doesn't handle that at all.
str len: c-str bytes: size-prefix bytes: difference:
1 1+1 (4) 4+1 (8) 4
2 2+1 (4) 4+2 (8) 4
3 3+1 (4) 4+3 (8) 4
4 4+1 (8) 4+4 (8) 0
5 5+1 (8) 4+5 (12) 4
6 6+1 (8) 4+6 (12) 4
7 7+1 (8) 4+7 (12) 4
8 8+1 (12) 4+8 (12) 0
So, for example: "ABCDE" as a C-style string would require 5 bytes for the string plus one for the null terminator, which would be satisfied by an allocation of 8 bytes. An equivalent length-prefixed string would require 4 bytes for the prefix plus 5 for the string, which would then be rounded up to 12.The only time the size-prefixed variant doesn't require more memory is when the string length is a multiple of 4. So the length-prefixed version requires an additional 3 bytes on average.
Worth every byte.
Similarly, neither of your "if let Some() = fork()" examples would fail as badly as the sloppy C program, right?
All the examples you could come up with of sloppy Rust code fail less poorly or make it harder to fail as poorly as the sloppy C code, and you were deliberately trying make your Rust code sloppy, whereas the sloppy C code was based on examples from the wild: http://rachelbythebay.com/w/2014/08/19/fork/
Still seems like a good argument that Rust's design is an improvement.
An example program written like that is not endemic to C or a logical consequence of its design. It's ignorance. Such examples frustrate me too. It's practically a straw-man argument.
> By the way, you can also write your own wrapper functions in C
And your own abstractions to provide more safety guarantees... it really depends on your judgement to balance risk, reward, and goals. It's possible to write a system that does all of the checks for you but you'll sacrifice the time and energy to do so (and will still find users blowing off their feet regardless of your best efforts).
Rust is doing very cool things but it's not going to make systems programming magically easier. Someone still had to write nix and you should still understand your programs.
Half the time the same developer has made a mistake in the same code.
With software being so complex, so full of human error -- almost any tool and practice that can help remedy this situation is welcome in my books.
I think it's entirely reasonable to write security-focused software in a highly restricted language with concrete formal semantics.
The problem with trivial examples is that they're often constructed to prove a point and don't represent what a real developer would write if they were to seriously consider the problem in C.
By the way, you can also write your own wrapper functions in C, that transform the return value into something like
Then Clang and GCC will warn you about missing switch cases.That said, the libc bindings in Rust are pretty low-level and a project that offers higher-level wrappers can be very helpful, so I hope my comment doesn't create the impression that I'm ripping on the project itself.