Debian Codegraph @

README.md

raw symbols SCIP
1 # `cascade`: Cascade expressions in Rust!
2 `cascade` is a macro library for Rust that makes it easy and ergonomic
3 to use cascade-like expressions, similar to Dart.
4
5 ```rust
6 #[macro_use]
7 extern crate cascade;
8
9 fn main() {
10 let cascaded_list = cascade! {
11 Vec::new();
12 ..push("Cascades");
13 ..push("reduce");
14 ..push("boilerplate");
15 };
16 println!("{:?}", cascaded_list); // Will print '["Cascades", "reduce", "boilerplate"]'
17 }
18 ```
19
20 This is only a small example of what `cascade` lets you do:
21 the `basic_cascades` example in this repository covers the other
22 cool features of the `cascade!` macro.
23
24 #### Why does this need to exist?
25 Cascades reduce boilerplate by eliminating the need for a 'temporary'
26 variable when making several method calls in a row, and it also
27 helps make struct member assignments look more ergonomic. For
28 example:
29 ```rust
30 #[macro_use]
31 extern crate cascade;
32
33 #[derive(Clone, Debug)]
34 struct Person {
35 pub name: String,
36 pub age: u32,
37 pub height: u32,
38 pub friend_names: Vec<String>
39 }
40
41 fn main() {
42 // Without cascades
43 let person = Person {
44 name: "John Smith",
45 age: 17,
46 height: 68, // 5' 8"
47 friend_names: {
48 let mut tmp_names = Vec::new();
49 tmp_names.push("James Smith".to_string());
50 tmp_names.push("Bob Jones".to_string());
51 tmp_names.push("Billy Jones".to_string());
52 tmp_names
53 }
54 };
55 // With cascades
56 let person = Person {
57 name: "John Smith",
58 age: 17,
59 height: 68,
60 friend_names: cascade! {
61 Vec::new();
62 ..push("James Smith".to_string());
63 ..push("Bob Jones".to_string());
64 ..push("Billy Jones".to_string());
65 }
66 };
67 // Cascades also let you do cool stuff like this
68 let person_one_year_later = cascade! {
69 person;
70 ..age += 1;
71 ..height += 2;
72 };
73 }
74 ```
75
76 In addition, cascades make it easier to design fluent interfaces.
77 No more returning `self` with every single function!
78
79 ### Changelog
80 **1.0.0**: `cascade` has reached 1.0! Here are some of the cool new features and syntax
81 changes made as a part of this:
82 - The syntax for binding variables has been changed to use `let` syntax. This makes it more
83 in-line with Rust syntax and also allows you to specify the type of a cascaded expression.
84 ```rust
85 cascade! {
86 // If you don't need to bind the statement to an identifier, you can use _
87 let _: Vec<u32> = vec![1,2,3].into_iter().map(|x| x + 1).collect();
88 ..push(1);
89 }
90 ```
91 - Statements no longer need `|` in front of them. You can just put the statement right in there, no prefix needed.
92 - You can return expressions from cascades, just like normal Rust blocks. By default,
93 cascades will already return the value of the cascaded variable. But if you want to return
94 a custom expression, you can put it at the end of a cascade block, with no semicolon.
95 ```rust
96 let has_more_than_three_elements = cascade! {
97 let v = vec![1,2,3];
98 ..push(4);
99 v.len() > 3
100 };
101 println!("{}", cascade! {
102 vec![1,2,3];
103 ..push(4);
104 ..into_iter().fold(0, |acc, x| acc + x)
105 });
106 ```
107 - Finally, you can have nested blocks within a cascade block. For example:
108 ```rust
109 cascade! {
110 vec![1,2,3];
111 {
112 let a = 1;
113 ..push(a);
114 };
115 }
116 ```
117
118 I hope you enjoy cascade `1.0`! Remember to leave any complaints or suggestions
119 on [the issue tracker](https://github.com/InquisitivePenguin/cascade/issues).
120
121 **0.1.3**: The ? operator now works with cascades, for scenarios like this:
122 ```rust
123 fn file_read() -> Result<SomeFileClass, ErrorMsg> {
124 cascade! {
125 SomeFileClass::create_file_reader("test.txt");
126 ..load()?;
127 ..check_validity()?;
128 }
129 }
130 ```
131
132 **0.1.2**: You can now chain methods together, like this:
133 ```rust
134 fn chain_example() {
135 cascade! {
136 FnChainTest::new();
137 ..chain().chain().chain();
138 }
139 }
140 ```
141
142 ### Credits
143 Written by Jane Lewis