]> git.lizzy.rs Git - rust.git/blob - tests/target/closure.rs
Merge pull request #1016 from rust-lang-nursery/try-double-indent
[rust.git] / tests / target / closure.rs
1 // Closures
2
3 fn main() {
4     let square = (|i: i32| i * i);
5
6     let commented = |// first
7                      a, // argument
8                      // second
9                      b: WithType, // argument
10                      // ignored
11                      _| {
12         (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb)
13     };
14
15     let block_body = move |xxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
16                            ref yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy| {
17         xxxxxxxxxxxxxxxxxxxxxxxxxxxxx + yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
18     };
19
20     let loooooooooooooong_name = |field| {
21         // format comments.
22         if field.node.attrs.len() > 0 {
23             field.node.attrs[0].span.lo
24         } else {
25             field.span.lo
26         }
27     };
28
29     let unblock_me = |trivial| closure();
30
31     let empty = |arg| {};
32
33     let simple = |arg| {
34         // comment formatting
35         foo(arg)
36     };
37
38     let test = || {
39         do_something();
40         do_something_else();
41     };
42
43     let arg_test =
44         |big_argument_name, test123| looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame();
45
46     let arg_test =
47         |big_argument_name, test123| looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame();
48
49     let simple_closure = move || -> () {};
50
51     let closure = |input: Ty| -> Option<String> { foo() };
52
53     let closure_with_return_type =
54         |aaaaaaaaaaaaaaaaaaaaaaarg1, aaaaaaaaaaaaaaaaaaaaaaarg2| -> Strong { "sup".to_owned() };
55
56     |arg1, arg2, _, _, arg3, arg4| {
57         let temp = arg4 + arg3;
58         arg2 * arg1 - temp
59     }
60 }
61
62 fn issue311() {
63     let func = |x| println!("{}", x);
64
65     (func)(0.0);
66 }
67
68 fn issue863() {
69     let closure = |x| match x {
70         0 => true,
71         _ => false,
72     } == true;
73 }
74
75 fn issue934() {
76     let hash: &Fn(&&Block) -> u64 = &|block| -> u64 {
77         let mut h = SpanlessHash::new(cx);
78         h.hash_block(block);
79         h.finish()
80     };
81
82     let hash: &Fn(&&Block) -> u64 = &|block| -> u64 {
83         let mut h = SpanlessHash::new(cx);
84         h.hash_block(block);
85         h.finish();
86     };
87 }
88
89 impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
90     pub fn eq_expr(&self, left: &Expr, right: &Expr) -> bool {
91         match (&left.node, &right.node) {
92             (&ExprBinary(l_op, ref ll, ref lr), &ExprBinary(r_op, ref rl, ref rr)) => {
93                 l_op.node == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr) ||
94                 swap_binop(l_op.node, ll, lr).map_or(false, |(l_op, ll, lr)| {
95                     l_op == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
96                 })
97             }
98         }
99     }
100 }