File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ //@ check-pass
2+ //@ edition: 2018
3+
4+ #![ feature( try_blocks) ]
5+ #![ crate_type = "lib" ]
6+
7+ // A few examples of code that only works with homogeneous `try`
8+
9+ pub fn index_or_zero ( x : & [ i32 ] , i : usize , j : usize ) -> i32 {
10+ // With heterogeneous `try` this fails because
11+ // it tries to call a method on a type variable.
12+ try { x. get ( i) ? + x. get ( j) ? } . unwrap_or ( 0 )
13+ }
14+
15+ pub fn do_nothing_on_errors ( a : & str , b : & str ) {
16+ // With heterogeneous `try` this fails because
17+ // an underscore pattern doesn't constrain the output type.
18+ let _ = try {
19+ let a = a. parse :: < i32 > ( ) ?;
20+ let b = b. parse :: < u32 > ( ) ?;
21+ println ! ( "{a} {b}" ) ;
22+ } ;
23+ }
24+
25+ pub fn print_error_once ( a : & str , b : & str ) {
26+ match try {
27+ let a = a. parse :: < i32 > ( ) ?;
28+ let b = b. parse :: < u32 > ( ) ?;
29+ ( a, b)
30+ } {
31+ Ok ( _pair) => { }
32+ // With heterogeneous `try` this fails because
33+ // nothing constrains the error type in `e`.
34+ Err ( e) => eprintln ! ( "{e}" ) ,
35+ }
36+ }
You can’t perform that action at this time.
0 commit comments