4 releases

0.1.3 Mar 23, 2019
0.1.2 Aug 14, 2018
0.1.1 Aug 13, 2018
0.1.0 Aug 13, 2018
Download history 32/week @ 2019-07-19 21/week @ 2019-07-26 16/week @ 2019-08-02 27/week @ 2019-08-09 331/week @ 2019-08-16 213/week @ 2019-08-23 131/week @ 2019-08-30 178/week @ 2019-09-06 172/week @ 2019-09-13 96/week @ 2019-09-20 40/week @ 2019-09-27 50/week @ 2019-10-04 110/week @ 2019-10-11 104/week @ 2019-10-18 63/week @ 2019-10-25

116 downloads per month
Used in 2 crates

MIT license

5KB

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.

#[macro_use]
extern crate cascade;

fn main() {
    let cascaded_list = cascade! {
      Vec::new();
      ..push("Cascades");
      ..push("reduce");
      ..push("boilerplate");
    };
    println!("{:?}", cascaded_list); // Will print '["Cascades", "reduce", "boilerplate"]'
}

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:

#[macro_use]
extern crate cascade;

#[derive(Clone, Debug)]
struct Person {
  pub name: String,
  pub age: u32,
  pub height: u32,
  pub friend_names: Vec<String>
}

fn main() {
    // Without cascades
    let person = Person {
      name: "John Smith",
      age: 17,
      height: 68, // 5' 8"
      friend_names: {
        let mut tmp_names = Vec::new();
        tmp_names.push("James Smith".to_string());
        tmp_names.push("Bob Jones".to_string());
        tmp_names.push("Billy Jones".to_string());
        tmp_names
      }
    };
    // With cascades
    let person = Person {
      name: "John Smith",
      age: 17,
      height: 68,
      friend_names: cascade! {
        Vec::new();
        ..push("James Smith".to_string());
        ..push("Bob Jones".to_string());
        ..push("Billy Jones".to_string());
      }
    };
    // Cascades also let you do cool stuff like this
    let person_one_year_later = cascade! {
      person;
      ..age += 1;
      ..height += 2;
    };
}

In addition, cascades make it easier to design fluent interfaces. No more returning self with every single function!

Credits

Written by Jackson Lewis

No runtime deps