@@ -127,22 +127,51 @@ impl<T> Option<T> {
127
127
#[ inline]
128
128
pub fn is_some ( & self ) -> bool { !self . is_none ( ) }
129
129
130
- /// Update an optional value by optionally running its content through a
131
- /// function that returns an option.
130
+ /// Returns `None` if the option is `None`, otherwise returns `optb`.
132
131
#[ inline]
133
- pub fn chain < U > ( self , f : & fn ( t : T ) -> Option < U > ) -> Option < U > {
132
+ pub fn and ( self , optb : Option < T > ) -> Option < T > {
134
133
match self {
135
- Some ( t) => f ( t) ,
136
- None => None
134
+ Some ( _) => optb,
135
+ None => None ,
136
+ }
137
+ }
138
+
139
+ /// Returns `None` if the option is `None`, otherwise calls and returns the
140
+ /// value of `f`.
141
+ #[ inline]
142
+ pub fn and_then ( self , f : & fn ( ) -> Option < T > ) -> Option < T > {
143
+ match self {
144
+ Some ( _) => f ( ) ,
145
+ None => None ,
137
146
}
138
147
}
139
148
140
- /// Returns the leftmost Some() value, or None if both are None .
149
+ /// Returns the option if it contains a value, otherwise returns `optb` .
141
150
#[ inline]
142
151
pub fn or ( self , optb : Option < T > ) -> Option < T > {
143
152
match self {
144
- Some ( opta) => Some ( opta) ,
145
- _ => optb
153
+ Some ( _) => self ,
154
+ None => optb
155
+ }
156
+ }
157
+
158
+ /// Returns the option if it contains a value, otherwise calls and returns the
159
+ /// value of `f`.
160
+ #[ inline]
161
+ pub fn or_else ( self , f : & fn ( ) -> Option < T > ) -> Option < T > {
162
+ match self {
163
+ Some ( _) => self ,
164
+ None => f ( ) ,
165
+ }
166
+ }
167
+
168
+ /// Update an optional value by optionally running its content through a
169
+ /// function that returns an option.
170
+ #[ inline]
171
+ pub fn chain < U > ( self , f : & fn ( T ) -> Option < U > ) -> Option < U > {
172
+ match self {
173
+ Some ( t) => f ( t) ,
174
+ None => None
146
175
}
147
176
}
148
177
@@ -509,6 +538,50 @@ mod tests {
509
538
let _y3 = y. take_unwrap ( ) ;
510
539
}
511
540
541
+ #[ test]
542
+ fn test_and ( ) {
543
+ let x: Option < int > = Some ( 1 ) ;
544
+ assert_eq ! ( x. and( Some ( 2 ) ) , Some ( 2 ) ) ;
545
+ assert_eq ! ( x. and( None ) , None ) ;
546
+
547
+ let x: Option < int > = None ;
548
+ assert_eq ! ( x. and( Some ( 2 ) ) , None ) ;
549
+ assert_eq ! ( x. and( None ) , None ) ;
550
+ }
551
+
552
+ #[ test]
553
+ fn test_and_then ( ) {
554
+ let x: Option < int > = Some ( 1 ) ;
555
+ assert_eq ! ( x. and_then( || Some ( 2 ) ) , Some ( 2 ) ) ;
556
+ assert_eq ! ( x. and_then( || None ) , None ) ;
557
+
558
+ let x: Option < int > = None ;
559
+ assert_eq ! ( x. and_then( || Some ( 2 ) ) , None ) ;
560
+ assert_eq ! ( x. and_then( || None ) , None ) ;
561
+ }
562
+
563
+ #[ test]
564
+ fn test_or ( ) {
565
+ let x: Option < int > = Some ( 1 ) ;
566
+ assert_eq ! ( x. or( Some ( 2 ) ) , Some ( 1 ) ) ;
567
+ assert_eq ! ( x. or( None ) , Some ( 1 ) ) ;
568
+
569
+ let x: Option < int > = None ;
570
+ assert_eq ! ( x. or( Some ( 2 ) ) , Some ( 2 ) ) ;
571
+ assert_eq ! ( x. or( None ) , None ) ;
572
+ }
573
+
574
+ #[ test]
575
+ fn test_or_else ( ) {
576
+ let x: Option < int > = Some ( 1 ) ;
577
+ assert_eq ! ( x. or_else( || Some ( 2 ) ) , Some ( 1 ) ) ;
578
+ assert_eq ! ( x. or_else( || None ) , Some ( 1 ) ) ;
579
+
580
+ let x: Option < int > = None ;
581
+ assert_eq ! ( x. or_else( || Some ( 2 ) ) , Some ( 2 ) ) ;
582
+ assert_eq ! ( x. or_else( || None ) , None ) ;
583
+ }
584
+
512
585
#[ test]
513
586
fn test_option_while_some( ) {
514
587
let mut i = 0 ;
0 commit comments