Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Try-Let
This is an implementation of a try-let
similar to the one proposed in
RFC #1303, as a syntax
extension.
NOTE: This is a syntax extension, and thus uses unstable features and will not work on stable rust.
Usage
try-let is implemented using a syntax extension instead of a macro, as
parsing the pattern expression in the way which try-let needs to is not
possible with a macro_rules!
macro.
To use the plugin, add #[plugin(try_let)]
to the top of the project,
like so:
The actual use is fairly similar to a let
expression:
try_let!;
The expression after else must diverge (e.g. via return
, continue
, break
or panic!
).
If you care about the values of other alternatives, you can match against them too:
// What do you know! It's `let x = try!(...)` implemented more verbosely!
try_let!;
This also handles more complex types than Some
and None
:
// ...
try_let!;
// a, c, e, and f are all bound here.
Why
This provides a simple way to avoid the rightward-shift of logic which performs a large number of faillible pattern matches in rust. This allows the main logic flow to continue without increasing the indent level, while handling errors with diverging logic.
How
a try_let!()
invocation expands to the following:
try_let!;
// ... becomes ...
let = match ... ;
try_let!;
// ... becomes ...
let = match ... ;
A note on None
and empty enum variants
A question which some people will be asking now is how are enum variants like
None
handled?
try_let!;
// ... becomes ...
let = match ...
None
isn't mistaken for a binding variable by try-let because of the dirty
little trick which try-let uses to function: which is that it is powered by
rust's style conventions. There is no way for the parser (which is all that the
syntax extension has access to) to determine whether a lone identifier in a
pattern is an empty enum variant like None
or a variable binding like x
.
This is determined later in the compiler, too late for this extension to use
that information.
Instead, the extension checks the first character of the identifier. If it is an ASCII capital, we assume it is a empty enum variant, and otherwise we assume it is a variable binding.