by 0xdeadb00f · 4y ago · 0 comments
> This function signatures tells us a lot already. It returns a Result, which means, not only do we know this can fail, we have to handle it. Either by panicking on error, with .unwrap() or .expect(), or by matching it against Result::Ok / Result::Err, or by bubbling it up with the ? operator.
What the hell? I'm not a rust dev. I have no idea what half of this means. "bubbling up"? What the fuck even is that?
Bubbling up means returning the error to the caller, and is general error handling jargon.
Rust is expressing the full complexity of a twisty maze of corner-cases built up from decades of filesystem features being slapped on with no care for each others' existence. Go just says "Sure, but you won't need most of that most of the time; here's a subset that usually works." So it solves most people's problem most of the time with far less words (with the trade-off that if you get burned, you get burned).
In Go, this looks like
thing, err := call()
if err != nil {
return nil, er
}
In Rust, this looks like let thing = call()?;When you throw an error and don't catch it, you just let the error immediately propagate up to the calling function. Think "return err"
>> Either by panicking on error, with .unwrap() or .expect()
This means fatally terminating the program immediately when an error is encountered in this spot.
>> or by matching it against Result::Ok / Result::Err
Result is an enum can be one of either Ok<T> or Err<E>. Pattern matching is one way of finding out which, and running code depending on which variant it is. It's just a fancy and convenient if statement, if that helps.