]>
Commit | Line | Data |
---|---|---|
cc61c64b XL |
1 | // Copyright 2017 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | #![feature(catch_expr)] | |
12 | ||
13 | struct catch {} | |
14 | ||
15 | pub fn main() { | |
16 | let catch_result = do catch { | |
17 | let x = 5; | |
18 | x | |
19 | }; | |
20 | assert_eq!(catch_result, 5); | |
21 | ||
22 | let mut catch = true; | |
23 | while catch { catch = false; } | |
24 | assert_eq!(catch, false); | |
25 | ||
26 | catch = if catch { false } else { true }; | |
27 | assert_eq!(catch, true); | |
28 | ||
29 | match catch { | |
30 | _ => {} | |
31 | }; | |
32 | ||
33 | let catch_err = do catch { | |
34 | Err(22)?; | |
35 | Ok(1) | |
36 | }; | |
37 | assert_eq!(catch_err, Err(22)); | |
38 | ||
39 | let catch_okay: Result<i32, i32> = do catch { | |
40 | if false { Err(25)?; } | |
41 | Ok::<(), i32>(())?; | |
42 | Ok(28) | |
43 | }; | |
44 | assert_eq!(catch_okay, Ok(28)); | |
45 | ||
46 | let catch_from_loop: Result<i32, i32> = do catch { | |
47 | for i in 0..10 { | |
48 | if i < 5 { Ok::<i32, i32>(i)?; } else { Err(i)?; } | |
49 | } | |
50 | Ok(22) | |
51 | }; | |
52 | assert_eq!(catch_from_loop, Err(5)); | |
53 | ||
54 | let cfg_init; | |
55 | let _res: Result<(), ()> = do catch { | |
56 | cfg_init = 5; | |
57 | Ok(()) | |
58 | }; | |
59 | assert_eq!(cfg_init, 5); | |
60 | ||
61 | let cfg_init_2; | |
62 | let _res: Result<(), ()> = do catch { | |
63 | cfg_init_2 = 6; | |
64 | Err(())?; | |
65 | Ok(()) | |
66 | }; | |
67 | assert_eq!(cfg_init_2, 6); | |
68 | ||
69 | let my_string = "test".to_string(); | |
70 | let res: Result<&str, ()> = do catch { | |
71 | Ok(&my_string) | |
72 | }; | |
73 | assert_eq!(res, Ok("test")); | |
74 | } |