Debian Codegraph @

examples/basic_cascades.rs

raw symbols SCIP
1 #[macro_use]
2 extern crate cascaderust-analyzer cargo cascade 1.0.1 cascade/;
3
4 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
5 struct Personrust-analyzer cargo cascade 1.0.1 Person# {
6 namerust-analyzer cargo cascade 1.0.1 Person#name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
7 agerust-analyzer cargo cascade 1.0.1 Person#age.: u32,
8 heightrust-analyzer cargo cascade 1.0.1 Person#height.: u32,
9 }
10
11 impl Personrust-analyzer cargo cascade 1.0.1 Person# {
12 pub fn blankrust-analyzer cargo cascade 1.0.1 impl#[Person]blank().() -> Personrust-analyzer cargo cascade 1.0.1 Person# {
13 Personrust-analyzer cargo cascade 1.0.1 Person# {
14 namerust-analyzer cargo cascade 1.0.1 Person#name.: "".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
15 agerust-analyzer cargo cascade 1.0.1 Person#age.: 0,
16 heightrust-analyzer cargo cascade 1.0.1 Person#height.: 0,
17 }
18 }
19 }
20
21 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
22 struct Chainrust-analyzer cargo cascade 1.0.1 Chain# {
23 linksrust-analyzer cargo cascade 1.0.1 Chain#links.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u32>,
24 }
25
26 impl Chainrust-analyzer cargo cascade 1.0.1 Chain# {
27 fn blankrust-analyzer cargo cascade 1.0.1 impl#[Chain]blank().() -> Chainrust-analyzer cargo cascade 1.0.1 Chain# {
28 Chainrust-analyzer cargo cascade 1.0.1 Chain# { linksrust-analyzer cargo cascade 1.0.1 Chain#links.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![] }
29 }
30 fn addrust-analyzer cargo cascade 1.0.1 impl#[Chain]add().(mut self, link: u32) -> Selfrust-analyzer cargo cascade 1.0.1 impl#[Chain] {
31 self.linksrust-analyzer cargo cascade 1.0.1 Chain#links..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(link);
32 self
33 }
34 }
35
36 #[allow(unused)]
37 fn mainrust-analyzer cargo cascade 1.0.1 main().() {
38 // Cascades can be used recursively!
39 let people = cascaderust-analyzer cargo cascade 1.0.1 cascade!! {
40 Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
41 ..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(cascaderust-analyzer cargo cascade 1.0.1 cascade!! {
42 Personrust-analyzer cargo cascade 1.0.1 Person#::blankrust-analyzer cargo cascade 1.0.1 impl#[Person]blank().();
43 ..namerust-analyzer cargo cascade 1.0.1 Person#name. = "John Smith".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
44 ..heightrust-analyzer cargo cascade 1.0.1 Person#height. = 72; // 6 feet, or 72 inches tall
45 ..agerust-analyzer cargo cascade 1.0.1 Person#age. = 34;
46 });
47 // This is what an expanded cascade looks like.
48 ..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().({
49 let mut __tmp = Personrust-analyzer cargo cascade 1.0.1 Person#::blankrust-analyzer cargo cascade 1.0.1 impl#[Person]blank().();
50 __tmp.namerust-analyzer cargo cascade 1.0.1 Person#name. = "Jason Smith".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
51 __tmp.heightrust-analyzer cargo cascade 1.0.1 Person#height. = 64;
52 __tmp.agerust-analyzer cargo cascade 1.0.1 Person#age. = 34;
53 __tmp
54 });
55 };
56 // Any expression works as the first statement of a cascade.
57 let other_person = cascaderust-analyzer cargo cascade 1.0.1 cascade!! {
58 people[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..clonerust-analyzer cargo cascade 1.0.1 impl#[Person][Clone]clone().();
59 ..namerust-analyzer cargo cascade 1.0.1 Person#name. = "Bob Smith".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
60 ..heightrust-analyzer cargo cascade 1.0.1 Person#height. = 66;
61 };
62 // You can also use +=, -=, *=, /= for operators
63 let another_person = cascaderust-analyzer cargo cascade 1.0.1 cascade!! {
64 other_person.clonerust-analyzer cargo cascade 1.0.1 impl#[Person][Clone]clone().();
65 ..namerust-analyzer cargo cascade 1.0.1 Person#name. = "Joan Smith".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
66 ..agerust-analyzer cargo cascade 1.0.1 Person#age. += 3;
67 ..heightrust-analyzer cargo cascade 1.0.1 Person#height. -= 4;
68 };
69 // You can put regular statements inside of a cascade macro
70 let yet_another_person = cascaderust-analyzer cargo cascade 1.0.1 cascade!! {
71 people[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..clonerust-analyzer cargo cascade 1.0.1 impl#[Person][Clone]clone().();
72 ..namerust-analyzer cargo cascade 1.0.1 Person#name. = "James Smith".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
73 ..agerust-analyzer cargo cascade 1.0.1 Person#age. = 27;
74 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Cascades in Rust are cool!");
75 ..heightrust-analyzer cargo cascade 1.0.1 Person#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 = cascaderust-analyzer cargo cascade 1.0.1 cascade!! {
79 let person = people[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..clonerust-analyzer cargo cascade 1.0.1 impl#[Person][Clone]clone().();
80 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("'person' was equal to: {:?}", person);
81 ..namerust-analyzer cargo cascade 1.0.1 Person#name. = "James Smith".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
82 ..heightrust-analyzer cargo cascade 1.0.1 Person#height. = ((person.heightrust-analyzer cargo cascade 1.0.1 Person#height. as f32) *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul(). 0.8) as u32;
83 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/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 = cascaderust-analyzer cargo cascade 1.0.1 cascade!! {
87 let ch = Chainrust-analyzer cargo cascade 1.0.1 Chain#::blankrust-analyzer cargo cascade 1.0.1 impl#[Chain]blank().();
88 ..addrust-analyzer cargo cascade 1.0.1 impl#[Chain]add().(5).addrust-analyzer cargo cascade 1.0.1 impl#[Chain]add().(6).addrust-analyzer cargo cascade 1.0.1 impl#[Chain]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 = cascaderust-analyzer cargo cascade 1.0.1 cascade!! {
94 Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
95 ..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(1);
96 ..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(2);
97 };
98
99 let has_more_than_three_elements = cascaderust-analyzer cargo cascade 1.0.1 cascade!! {
100 let v = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1,2,3];
101 ..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(4);
102 v.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() >rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). 3
103 };
104
105 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{}", cascaderust-analyzer cargo cascade 1.0.1 cascade!! {
106 vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1,2,3];
107 ..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(4);
108 ..into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().foldrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/into_iter/impl#[`IntoIter<T, A>`][Iterator]fold().(0, |acc, x| acc +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add(). x)
109 });
110
111 cascaderust-analyzer cargo cascade 1.0.1 cascade!! {
112 let _: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u32> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1,2,3].into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|x| x +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add(). 1).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
113 ..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(1);
114 };
115
116 option_cascade_testrust-analyzer cargo cascade 1.0.1 option_cascade_test().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
117 }
118
119 // As of version 0.1.3, you can use the ? operator after a .. statement.
120 fn option_cascade_testrust-analyzer cargo cascade 1.0.1 option_cascade_test().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), ()>, ()> {
121 let question_mark_operator_example: Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), ()>, ()> = cascaderust-analyzer cargo cascade 1.0.1 cascade!! {
122 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()));
123 ..unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()?;
124 };
125 return question_mark_operator_example;
126 }