@@ -5,7 +5,7 @@ or-rs is a library that extends the syntax to allow different types to be return
5
5
## Usage
6
6
Currently, the ` or_gen! ` macro supports Rust's ` if ` and ` match ` statement.
7
7
8
- ### if expression
8
+ ** if expression**
9
9
``` rust
10
10
#![feature(proc_macro_hygiene)] // for now, you have to add this unstable feature flag
11
11
@@ -16,11 +16,11 @@ fn main() {
16
16
// you have to add a type annotation **explicitly** for `x`.
17
17
#[or_gen]
18
18
let x : Or3 <i32 , String , f32 > = if true {
19
- 3
19
+ 3 // this branch returns `i32`
20
20
} else if false {
21
- " hello" . to_string ()
21
+ " hello" . to_string () // this branch returns `String`
22
22
} else {
23
- 3.0
23
+ 3.0 // this branch returns `f32`
24
24
};
25
25
26
26
// Check if `x` is of type t2(=String)
@@ -32,7 +32,7 @@ fn main() {
32
32
}
33
33
```
34
34
35
- ### match expression
35
+ ** match expression**
36
36
``` rust
37
37
#![feature(proc_macro_hygiene)] // for now, you have to add this unstable feature flag
38
38
@@ -43,9 +43,9 @@ fn main() {
43
43
// you have to add a type annotation **explicitly** for `x`.
44
44
#[or_gen]
45
45
let x : Or3 <i32 , f32 , String > = match 42 {
46
- 1 => 22 ,
47
- 10 => 3.2 ,
48
- _ => " hello" . to_string (),
46
+ 42 => 3 , // this branch returns `i32`
47
+ 10 => 3.2 , // this branch returns `f32`
48
+ _ => " hello" . to_string (), // this branch returns `String`
49
49
};
50
50
51
51
// map works **only** when the inner value of Or is i32.
@@ -67,7 +67,6 @@ let s = if true {
67
67
} else if false {
68
68
" tofs" . to_string ()
69
69
} else {
70
- 11 ;
71
70
3.0 // ERROR! `if` and `else` have incompatible types.
72
71
};
73
72
```
@@ -84,7 +83,6 @@ let s: Or3<i32, String, f32> = if true {
84
83
} else if false {
85
84
" tofs" . to_string ()
86
85
} else {
87
- 11 ;
88
86
3.0
89
87
};
90
88
// -> OK: This compiles!
@@ -100,7 +98,6 @@ let s: Or3<i32, String, f32> = if true {
100
98
Or3 :: <i32 , String , f32 >:: T2 (" tofs" . to_string ())
101
99
} else {
102
100
{
103
- 11 ;
104
101
Or3 :: <i32 , String , f32 >:: T3 (3.0 )
105
102
}
106
103
};
0 commit comments