.unwrap_or_ruin_christmas()can you explain the joke, i work at cloudflare
i work at cloudflare
Thank you for the easier captchas, and pre-emptively damn you for whatever evil thing CloudFlare will eventually do with their MITM access to everything.
Rust has many container-like objects that may or may contain a value, like Box. Most of these have an unwrap() method that either obtains the inner value or panics the whole application.
I reckon the person you’re replying to knows exactly what unwrap is, because the last big famous Cloudflare outage was caused by it. They were making a joke of their own
Whoosh
Box is a bad example; it always has a value and can’t be unwrapped. It’s Result<T> and Option<T> that are the primary wrappers.
Result is for Errors, and Option is for nullables. If you consider it that way, the “issues” with unwrap are identical to other languages when errors and nulls aren’t properly handled.
sounds like an error handling issue
Yes it is. Typically you’d do some pattern matching to handle every possible case, but Unwrap is often used as a shortcut.
Is this just like the equivalent of a getter method in C++?
It’s worse than just exceptions in C++. There’s (almost) no way for the caller of your function to catch it. It’s a bit like this snippet:
std::optional<int> foo = <...>; try { return foo.value(); } catch(const std::bad_optional_access& e) { std::cout << e.what() << std::endl; abort(); }It’s the
abortthat is the crux of the issue here. Usually you would pass thestd::optionalup/down the call stack. If you don’t control the types (e.g. using a library or a framework) you’d come up with some “default” value instead, like this:std::optional<int> foo = <...>; return foo.value_or(123);Or in Rust:
let foo : Option<i32> = <...>; return foo.unwrap_or(123);But sometimes there’s no good “default” value, and then you have to resort to just
unwrap-ing the value, and accepting that the entire program will abort when that function call fails. Usually this is a sign of poor engineering somewhere, likely in a library you’re using, and should be fixed; but sometimes you don’t have the time to fix it, and then it ends up in production.It’s more like a method that can throw an exception. Rust doesn’t really have exceptions, but if you have a Result<T> or Option<T> type you can Unwrap it to get just the T. But if there’s no T to get (in the case of an Error type for Result for None for Option) the call panics.
so you have to wrap everything in a try/catch?
It’s worse than that. In C++, if you fail to catch an exception, then std::terminate() is called. In Rust the only options are roughly equivalent to C++ noexcept, or std::terminate() [panic in Rust]. There’s nothing in between.
i’ve been meaning to get into rust; but i’m learning that i lack motivation if it’s not part of my job and becoming a sysadmin again doesn’t require it.
Not really, because rust doesn’t have exceptions. Instead you are encouraged to handle every possible case with pattern matching. For example:
fn maybe_add_one(number: Option<u8>) -> u8 { match number { None => 0, Some(i) => i + 1, } }Option<u8> is a type which can either be some 8bit unsigned integer, or none. It’s conceptually similar to a
Nullable<int>in C#.In C# you could correctly implement this like:
public int MaybeAddOne(int? number) { if (number.HasValue) { return number.Value + 1; } return 0; }In rust, you can call Unwrap on an option to get the underlying value, but it will panic if the value is None (because None isn’t a u8):
fn maybe_add_one(number: Option<u8>) -> u8 { number.unwrap() + 1 }In some cases unwrap could be useful if you don’t care about a panic or if you know the value can’t be
None. Sometimes it’s just used as a shortcut. You can likewise do this in C#:public int MaybeAddOne(int? number) { return number.Value + 1; }But this throws an exception if number is null.
A panic isn’t the same as an exception though, you can’t ‘catch’ a panic, it’s unrecoverable and the program will terminate more-or-less immediately.
Rust provides a generic type
Result<T, E>, T being a successful result and E being some error type, which you are encouraged to use along with pattern matching to make sure all cases are handled.Rust does not have exceptions. You never have to try/catch. Functions usually encode the possible failures in their types, so you’d have something like this C++ snippet:
struct HttpError { std::string message; int responseCode; } struct FsError { std::string message; } typedef std::variant<HttpError, IoError> FileDownloadError; std::variant<std::filesystem::path, FileDownloadError> downloadFile(std::string url) { /* ... */ }And then the caller of
downloadFilehas to decide what to do if it returns an error:auto res = std::visit(overloaded { [](FileDownloadError e) { /* ignore error, or handle it somehow, and return a new path */ }, [](std::filesystem::path p) { return p; } }, downloadFile(url)); /* res is guaranteed to be a valid std::filesystem::path, you don't have to care about errors from downloadFile anymore */However, Rust makes this whole thing a lot easier, by providing syntax sugar, and there are helper libraries that reduce the boilerplate. You usually end up with something like
#[derive(Error, Debug)] enum FileDownloadError { #[error("HTTP request failed")] HttpError(http::status::StatusCode), #[error("Filesystem error: {0}") FSError(#[from] std::io::Error), } fn download_file(String url) -> Result<Path, FileDownloadError> {/* ... */}(notice the
#[from], which forwards the error message etc from thestd::io::Errortype)The
Resulttype is kind of like astd::variantwith two template arguments, and, mostly by convention, the first one denotes the successful execution, while the second one is the error type.Resulthas a bunch of methods defined on it that help you with error handling.Consumer code is something like this:
let res : Path = download_file(url).unwrap_or_else(|e| { /* Ignore the error or handle it. You have to return a new path here */ }); /* res is guaranteed to be a valid Path, you don't have to care about errors from download_file anymore */Or
let res : Path = download_file(url)?; /* res is guaranteed to be a valid Path, you don't have to care about errors from download_file anymore */Which will just forward the error to your caller (but your function has to return
Resultas well), or proceed with the execution if the function succeeded.Finally,
download_file(url).unwrap()is if you can neither ignore, nor handle, nor pass the error to the caller. It will abort if the function fails in any way, and there’s no (practical) way to catch that abort.
i work at cloudflare
I shouldn’t worry about it.
It is Christmas morning and I am fucking panicking




