1
1
/*!
2
- * Operations on the ubiquitous `option` type.
3
- *
4
- * Type `option` represents an optional value.
5
- *
6
- * Every `Option<T>` value can either be `Some(T)` or `none`. Where in other
7
- * languages you might use a nullable type, in Rust you would use an option
8
- * type.
9
- */
2
+
3
+ Operations on the ubiquitous `Option` type.
4
+
5
+ Type `Option` represents an optional value.
6
+
7
+ Every `Option<T>` value can either be `Some(T)` or `None`. Where in other
8
+ languages you might use a nullable type, in Rust you would use an option
9
+ type.
10
+
11
+ Options are most commonly used with pattern matching to query the presence
12
+ of a value and take action, always accounting for the `None` case.
13
+
14
+ # Example
15
+
16
+ ~~~
17
+ let msg = Some(~"howdy");
18
+
19
+ // Take a reference to the contained string
20
+ match msg {
21
+ Some(ref m) => io::println(m),
22
+ None => ()
23
+ }
24
+
25
+ // Remove the contained string, destroying the Option
26
+ let unwrapped_msg = match move msg {
27
+ Some(move m) => m,
28
+ None => ~"default message"
29
+ };
30
+ ~~~
31
+
32
+ */
10
33
11
34
// NB: transitionary, de-mode-ing.
12
35
#[ warn( deprecated_mode) ] ;
@@ -22,12 +45,19 @@ pub enum Option<T> {
22
45
23
46
pub pure fn get < T : Copy > ( opt : & Option < T > ) -> T {
24
47
/*!
25
- * Gets the value out of an option
26
- *
27
- * # Failure
28
- *
29
- * Fails if the value equals `none`
30
- */
48
+ Gets the value out of an option
49
+
50
+ # Failure
51
+
52
+ Fails if the value equals `None`
53
+
54
+ # Safety note
55
+
56
+ In general, because this function may fail, its use is discouraged
57
+ (calling `get` on `None` is akin to dereferencing a null pointer).
58
+ Instead, prefer to use pattern matching and handle the `None`
59
+ case explicitly.
60
+ */
31
61
32
62
match * opt {
33
63
Some ( copy x) => return x,
@@ -37,11 +67,18 @@ pub pure fn get<T: Copy>(opt: &Option<T>) -> T {
37
67
38
68
pub pure fn get_ref < T > ( opt : & r/Option < T > ) -> & r /T {
39
69
/*!
40
- * Gets an immutable reference to the value inside an option.
41
- *
42
- * # Failure
43
- *
44
- * Fails if the value equals `none`
70
+ Gets an immutable reference to the value inside an option.
71
+
72
+ # Failure
73
+
74
+ Fails if the value equals `None`
75
+
76
+ # Safety note
77
+
78
+ In general, because this function may fail, its use is discouraged
79
+ (calling `get` on `None` is akin to dereferencing a null pointer).
80
+ Instead, prefer to use pattern matching and handle the `None`
81
+ case explicitly.
45
82
*/
46
83
match * opt {
47
84
Some ( ref x) => x,
@@ -154,20 +191,37 @@ pub pure fn iter<T>(opt: &Option<T>, f: fn(x: &T)) {
154
191
#[ inline( always) ]
155
192
pub pure fn unwrap < T > ( opt : Option < T > ) -> T {
156
193
/*!
157
- * Moves a value out of an option type and returns it.
158
- *
159
- * Useful primarily for getting strings, vectors and unique pointers out
160
- * of option types without copying them.
194
+ Moves a value out of an option type and returns it.
195
+
196
+ Useful primarily for getting strings, vectors and unique pointers out
197
+ of option types without copying them.
198
+
199
+ # Failure
200
+
201
+ Fails if the value equals `None`.
202
+
203
+ # Safety note
204
+
205
+ In general, because this function may fail, its use is discouraged.
206
+ Instead, prefer to use pattern matching and handle the `None`
207
+ case explicitly.
161
208
*/
162
209
match move opt {
163
210
Some ( move x) => move x,
164
211
None => fail ~"option:: unwrap none"
165
212
}
166
213
}
167
214
168
- /// The ubiquitous option dance.
169
215
#[ inline( always) ]
170
216
pub fn swap_unwrap < T > ( opt : & mut Option < T > ) -> T {
217
+ /*!
218
+ The option dance. Moves a value out of an option type and returns it,
219
+ replacing the original with `None`.
220
+
221
+ # Failure
222
+
223
+ Fails if the value equals `None`.
224
+ */
171
225
if opt. is_none ( ) { fail ~"option:: swap_unwrap none" }
172
226
unwrap ( util:: replace ( opt, None ) )
173
227
}
@@ -201,18 +255,38 @@ impl<T> &Option<T> {
201
255
pure fn iter ( f : fn ( x : & T ) ) { iter ( self , f) }
202
256
/// Maps a `some` value from one type to another by reference
203
257
pure fn map < U > ( f : fn ( x : & T ) -> U ) -> Option < U > { map ( self , f) }
204
- /// Gets an immutable reference to the value inside a `some`.
258
+ /**
259
+ Gets an immutable reference to the value inside an option.
260
+
261
+ # Failure
262
+
263
+ Fails if the value equals `None`
264
+
265
+ # Safety note
266
+
267
+ In general, because this function may fail, its use is discouraged
268
+ (calling `get` on `None` is akin to dereferencing a null pointer).
269
+ Instead, prefer to use pattern matching and handle the `None`
270
+ case explicitly.
271
+ */
205
272
pure fn get_ref ( ) -> & self /T { get_ref ( self ) }
206
273
}
207
274
208
275
impl < T : Copy > Option < T > {
209
276
/**
210
- * Gets the value out of an option
211
- *
212
- * # Failure
213
- *
214
- * Fails if the value equals `none`
215
- */
277
+ Gets the value out of an option
278
+
279
+ # Failure
280
+
281
+ Fails if the value equals `None`
282
+
283
+ # Safety note
284
+
285
+ In general, because this function may fail, its use is discouraged
286
+ (calling `get` on `None` is akin to dereferencing a null pointer).
287
+ Instead, prefer to use pattern matching and handle the `None`
288
+ case explicitly.
289
+ */
216
290
pure fn get ( ) -> T { get ( & self ) }
217
291
pure fn get_default ( def : T ) -> T { get_default ( & self , def) }
218
292
/**
0 commit comments