@@ -25,6 +25,17 @@ pure fn get<T: copy>(opt: option<T>) -> T {
25
25
alt opt { some( x) { ret x; } none { fail "option none" ; } }
26
26
}
27
27
28
+ pure fn expect<T : copy>( opt: option<T >, reason: str) -> T {
29
+ #[ doc = "
30
+ Gets the value out of an option, printing a specified message on failure
31
+
32
+ # Failure
33
+
34
+ Fails if the value equals `none`
35
+ " ] ;
36
+ alt opt { some( x) { x } none { fail reason; } }
37
+ }
38
+
28
39
pure fn map<T , U : copy>( opt: option<T >, f: fn ( T ) -> U ) -> option<U > {
29
40
#[ doc = "Maps a `some` value from one type to another" ] ;
30
41
@@ -94,18 +105,18 @@ impl extensions<T> for option<T> {
94
105
Update an optional value by optionally running its content through a
95
106
function that returns an option.
96
107
" ]
97
- fn chain < U > ( f : fn ( T ) -> option < U > ) -> option < U > { chain ( self , f) }
108
+ pure fn chain < U > ( f : fn ( T ) -> option < U > ) -> option < U > { chain ( self , f) }
98
109
#[ doc = "Applies a function to the contained value or returns a default" ]
99
- fn map_default < U : copy > ( def : U , f : fn ( T ) -> U ) -> U
110
+ pure fn map_default < U : copy > ( def : U , f : fn ( T ) -> U ) -> U
100
111
{ map_default ( self , def, f) }
101
112
#[ doc = "Performs an operation on the contained value or does nothing" ]
102
- fn iter ( f : fn ( T ) ) { iter ( self , f) }
113
+ pure fn iter ( f : fn ( T ) ) { iter ( self , f) }
103
114
#[ doc = "Returns true if the option equals `none`" ]
104
- fn is_none ( ) -> bool { is_none ( self ) }
115
+ pure fn is_none ( ) -> bool { is_none ( self ) }
105
116
#[ doc = "Returns true if the option contains some value" ]
106
- fn is_some ( ) -> bool { is_some ( self ) }
117
+ pure fn is_some ( ) -> bool { is_some ( self ) }
107
118
#[ doc = "Maps a `some` value from one type to another" ]
108
- fn map < U : copy > ( f : fn ( T ) -> U ) -> option < U > { map ( self , f) }
119
+ pure fn map < U : copy > ( f : fn ( T ) -> U ) -> option < U > { map ( self , f) }
109
120
}
110
121
111
122
impl extensions < T : copy > for option < T > {
@@ -116,8 +127,16 @@ impl extensions<T: copy> for option<T> {
116
127
117
128
Fails if the value equals `none`
118
129
" ]
119
- fn get ( ) -> T { get ( self ) }
120
- fn get_default ( def : T ) -> T { get_default ( self , def) }
130
+ pure fn get ( ) -> T { get ( self ) }
131
+ pure fn get_default ( def : T ) -> T { get_default ( self , def) }
132
+ #[ doc = "
133
+ Gets the value out of an option, printing a specified message on failure
134
+
135
+ # Failure
136
+
137
+ Fails if the value equals `none`
138
+ " ]
139
+ pure fn expect < T : copy > ( reason : str ) -> T { expect ( self , reason) }
121
140
}
122
141
123
142
#[ test]
0 commit comments