项目作者: InquisitivePenguin

项目描述 :
Dart-like cascade expressions in Rust
高级语言: Rust
项目地址: git://github.com/InquisitivePenguin/cascade.git
创建时间: 2018-08-13T15:49:40Z
项目社区:https://github.com/InquisitivePenguin/cascade

开源协议:MIT License

下载


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.

  1. #[macro_use]
  2. extern crate cascade;
  3. fn main() {
  4. let cascaded_list = cascade! {
  5. Vec::new();
  6. ..push("Cascades");
  7. ..push("reduce");
  8. ..push("boilerplate");
  9. };
  10. println!("{:?}", cascaded_list); // Will print '["Cascades", "reduce", "boilerplate"]'
  11. }

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:

  1. #[macro_use]
  2. extern crate cascade;
  3. #[derive(Clone, Debug)]
  4. struct Person {
  5. pub name: String,
  6. pub age: u32,
  7. pub height: u32,
  8. pub friend_names: Vec<String>
  9. }
  10. fn main() {
  11. // Without cascades
  12. let person = Person {
  13. name: "John Smith",
  14. age: 17,
  15. height: 68, // 5' 8"
  16. friend_names: {
  17. let mut tmp_names = Vec::new();
  18. tmp_names.push("James Smith".to_string());
  19. tmp_names.push("Bob Jones".to_string());
  20. tmp_names.push("Billy Jones".to_string());
  21. tmp_names
  22. }
  23. };
  24. // With cascades
  25. let person = Person {
  26. name: "John Smith",
  27. age: 17,
  28. height: 68,
  29. friend_names: cascade! {
  30. Vec::new();
  31. ..push("James Smith".to_string());
  32. ..push("Bob Jones".to_string());
  33. ..push("Billy Jones".to_string());
  34. }
  35. };
  36. // Cascades also let you do cool stuff like this
  37. let person_one_year_later = cascade! {
  38. person;
  39. ..age += 1;
  40. ..height += 2;
  41. };
  42. }

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 let syntax. This makes it more
    in-line with Rust syntax and also allows you to specify the type of a cascaded expression.
    1. cascade! {
    2. // If you don't need to bind the statement to an identifier, you can use _
    3. let _: Vec<u32> = vec![1,2,3].into_iter().map(|x| x + 1).collect();
    4. ..push(1);
    5. }
  • 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.
    1. let has_more_than_three_elements = cascade! {
    2. let v = vec![1,2,3];
    3. ..push(4);
    4. v.len() > 3
    5. };
    6. println!("{}", cascade! {
    7. vec![1,2,3];
    8. ..push(4);
    9. ..into_iter().fold(0, |acc, x| acc + x)
    10. });
  • Finally, you can have nested blocks within a cascade block. For example:
    1. cascade! {
    2. vec![1,2,3];
    3. {
    4. let a = 1;
    5. ..push(a);
    6. };
    7. }

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:

  1. fn file_read() -> Result<SomeFileClass, ErrorMsg> {
  2. cascade! {
  3. SomeFileClass::create_file_reader("test.txt");
  4. ..load()?;
  5. ..check_validity()?;
  6. }
  7. }

0.1.2: You can now chain methods together, like this:

  1. fn chain_example() {
  2. cascade! {
  3. FnChainTest::new();
  4. ..chain().chain().chain();
  5. }
  6. }

Credits

Written by Jane Lewis