here's some rust theory mumbo jumbo: when you look at a function's signature (ignoring doc comments), you can understand the following information:
- the call convention
extern "C"or native rust - the function arguments
- the presence of unsafety contracts
- argument types
- generic arguments (optional)
- generic bounds
- constness
- return type (and return control flow e.g.
!) - variadicness
you know what the function signature doesn't communicate well? panics. all functions have the ability of implicitly panicking. a panic is a portion of code in the rust core library that gives any function the ability to just woop jump to that portion of code, terminate the entire program because it failed and not give 2 fucks about it. sure, it is better to panic than cause undefined behavior or memory unsafety, but we can do better.
imagine this: every function signature had an implicit Option<T> wrapping its return type. and if you don't handle this Option::None case, it's fine, because it bubbles out from the function (as if it had an implicit ? at the end of where it's been created at). Let's rename this Option to MayFail. now all rust functions have an implicit MayFail that can be optionally handled for failures. if you don't handle it, it's fine. the program will just go on with its day. this MayFail has no size overhead, and is just a transparent wrapper. does any of this feel familiar? lets try using it in practice:
)