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.
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.
11 // compile-flags: --edition 2018
13 #![feature(try_blocks)]
18 let catch_result: Option<_> = try {
22 assert_eq!(catch_result, Some(5));
25 while catch { catch = false; }
26 assert_eq!(catch, false);
28 catch = if catch { false } else { true };
29 assert_eq!(catch, true);
35 let catch_err: Result<_, i32> = try {
39 assert_eq!(catch_err, Err(22));
41 let catch_okay: Result<i32, i32> = try {
42 if false { Err(25)?; }
46 assert_eq!(catch_okay, Ok(28));
48 let catch_from_loop: Result<i32, i32> = try {
50 if i < 5 { Ok::<i32, i32>(i)?; } else { Err(i)?; }
54 assert_eq!(catch_from_loop, Err(5));
57 let _res: Result<(), ()> = try {
60 assert_eq!(cfg_init, 5);
63 let _res: Result<(), ()> = try {
67 assert_eq!(cfg_init_2, 6);
69 let my_string = "test".to_string();
70 let res: Result<&str, ()> = try {
71 // Unfortunately, deref doesn't fire here (#49356)
74 assert_eq!(res, Ok("test"));
76 let my_opt: Option<_> = try { () };
77 assert_eq!(my_opt, Some(()));
79 let my_opt: Option<_> = try { };
80 assert_eq!(my_opt, Some(()));