cascade: Cascade expressions in Rust!
cascade is a macro library for Rust that makes it easy and ergonomic
to use cascade-like expressions, similar to Dart.
extern crate cascade;
This is only a small example of what cascade lets you do:
the basic_cascades example in this repository covers the other
cool features of the cascade! macro.
Why does this need to exist?
Cascades reduce boilerplate by eliminating the need for a 'temporary' variable when making several method calls in a row, and it also helps make struct member assignments look more ergonomic. For example:
extern crate cascade;
In addition, cascades make it easier to design fluent interfaces.
No more returning self with every single function!
Changelog
1.0.0: cascade has reached 1.0! Here are some of the cool new features and syntax
changes made as a part of this:
- The syntax for binding variables has been changed to use
letsyntax. This makes it more in-line with Rust syntax and also allows you to specify the type of a cascaded expression.
cascade!
- Statements no longer need
|in front of them. You can just put the statement right in there, no prefix needed. - You can return expressions from cascades, just like normal Rust blocks. By default, cascades will already return the value of the cascaded variable. But if you want to return a custom expression, you can put it at the end of a cascade block, with no semicolon.
let has_more_than_three_elements = cascade! ;
println!;
- Finally, you can have nested blocks within a cascade block. For example:
cascade!
I hope you enjoy cascade 1.0! Remember to leave any complaints or suggestions
on the issue tracker.
0.1.3: The ? operator now works with cascades, for scenarios like this:
0.1.2: You can now chain methods together, like this:
Credits
Written by Jane Lewis