FILE* f = fopen("somefile", "r");
fwrite("aha", 4, 1, f); //
In Rust (pardon my rust, I'm totally ignorant), it'd be: let f = fopen("somefile", "r");
fwrite("aha", f)?;
The compiler would know that fopen may return an error, and forbid me from running unchecked code. Nice!I'd miss, however, the ability to handle all errors occurring from a segment of code in the same place, stuff that exceptions allow for. Pythonish example
try:
db = pgsql.connect(localhost)
stmt = db.prepare('INSERT INTO log VALUES(?,?)')
stmt.execute(('debug', 'Note to self: debug logs are noncritical'))
except Exception,e:
console.write('Could not write log to database %s' % (str(e)))
Sometimes error recovery is the same for all of the segment. I realize one could extract a function for the commonly recovered code, but this may lead to a too-many-small-functions-with-one-caller(tm) smell.Exceptions aren't the only way to achieve this. If Rust wants to keep return values as the error mechanism, perhaps it could find a way of allowing recovery to happen in the same place for a segment of code.