| 1 | #[macro_use] |
| 2 | extern crate cascade; |
| 3 | |
| 4 | #[derive(Clone, Debug)] |
| 5 | struct Person { |
| 6 | name: String, |
| 7 | age: u32, |
| 8 | height: u32, |
| 9 | } |
| 10 | |
| 11 | impl Person { |
| 12 | pub fn blank() -> Person { |
| 13 | Person { |
| 14 | name: "".to_string(), |
| 15 | age: 0, |
| 16 | height: 0, |
| 17 | } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | #[derive(Clone, Debug)] |
| 22 | struct Chain { |
| 23 | links: Vec<u32>, |
| 24 | } |
| 25 | |
| 26 | impl Chain { |
| 27 | fn blank() -> Chain { |
| 28 | Chain { links: vec![] } |
| 29 | } |
| 30 | fn add(mut self, link: u32) -> Self { |
| 31 | self.links.push(link); |
| 32 | self |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | #[allow(unused)] |
| 37 | fn main() { |
| 38 | // Cascades can be used recursively! |
| 39 | let people = cascade! { |
| 40 | Vec::new(); |
| 41 | ..push(cascade! { |
| 42 | Person::blank(); |
| 43 | ..name = "John Smith".to_string(); |
| 44 | ..height = 72; // 6 feet, or 72 inches tall |
| 45 | ..age = 34; |
| 46 | }); |
| 47 | // This is what an expanded cascade looks like. |
| 48 | ..push({ |
| 49 | let mut __tmp = Person::blank(); |
| 50 | __tmp.name = "Jason Smith".to_string(); |
| 51 | __tmp.height = 64; |
| 52 | __tmp.age = 34; |
| 53 | __tmp |
| 54 | }); |
| 55 | }; |
| 56 | // Any expression works as the first statement of a cascade. |
| 57 | let other_person = cascade! { |
| 58 | people[0].clone(); |
| 59 | ..name = "Bob Smith".to_string(); |
| 60 | ..height = 66; |
| 61 | }; |
| 62 | // You can also use +=, -=, *=, /= for operators |
| 63 | let another_person = cascade! { |
| 64 | other_person.clone(); |
| 65 | ..name = "Joan Smith".to_string(); |
| 66 | ..age += 3; |
| 67 | ..height -= 4; |
| 68 | }; |
| 69 | // You can put regular statements inside of a cascade macro |
| 70 | let yet_another_person = cascade! { |
| 71 | people[0].clone(); |
| 72 | ..name = "James Smith".to_string(); |
| 73 | ..age = 27; |
| 74 | println!("Cascades in Rust are cool!"); |
| 75 | ..height -= 3; |
| 76 | }; |
| 77 | // You can bind the initial value of the cascade to an identifier, which reflects the current state of the cascaded value. |
| 78 | let one_more_person = cascade! { |
| 79 | let person = people[0].clone(); |
| 80 | println!("'person' was equal to: {:?}", person); |
| 81 | ..name = "James Smith".to_string(); |
| 82 | ..height = ((person.height as f32) * 0.8) as u32; |
| 83 | println!("'person' is now equal to: {:?}", person); |
| 84 | }; |
| 85 | // As of version 0.1.2, you can also chain methods together. Observe: |
| 86 | let method_chain_example = cascade! { |
| 87 | let ch = Chain::blank(); |
| 88 | ..add(5).add(6).add(7); // In this case, ch is consumed here. So we have to shadow ch to avoid an error. Obviously, this isn't the most useful way to method-chain. |
| 89 | let ch = (); |
| 90 | }; |
| 91 | |
| 92 | // You can have nested blocks within the cascade |
| 93 | let block_example = cascade! { |
| 94 | Vec::new(); |
| 95 | ..push(1); |
| 96 | ..push(2); |
| 97 | }; |
| 98 | |
| 99 | let has_more_than_three_elements = cascade! { |
| 100 | let v = vec![1,2,3]; |
| 101 | ..push(4); |
| 102 | v.len() > 3 |
| 103 | }; |
| 104 | |
| 105 | println!("{}", cascade! { |
| 106 | vec![1,2,3]; |
| 107 | ..push(4); |
| 108 | ..into_iter().fold(0, |acc, x| acc + x) |
| 109 | }); |
| 110 | |
| 111 | cascade! { |
| 112 | let _: Vec<u32> = vec![1,2,3].into_iter().map(|x| x + 1).collect(); |
| 113 | ..push(1); |
| 114 | }; |
| 115 | |
| 116 | option_cascade_test().unwrap().unwrap(); |
| 117 | } |
| 118 | |
| 119 | // As of version 0.1.3, you can use the ? operator after a .. statement. |
| 120 | fn option_cascade_test() -> Result<Result<(), ()>, ()> { |
| 121 | let question_mark_operator_example: Result<Result<(), ()>, ()> = cascade! { |
| 122 | Ok(Ok(())); |
| 123 | ..unwrap()?; |
| 124 | }; |
| 125 | return question_mark_operator_example; |
| 126 | } |