11/*!
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+ */
1033
1134// NB: transitionary, de-mode-ing.
1235#[ warn( deprecated_mode) ] ;
@@ -22,12 +45,19 @@ pub enum Option<T> {
2245
2346pub pure fn get < T : Copy > ( opt : & Option < T > ) -> T {
2447 /*!
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+ */
3161
3262 match * opt {
3363 Some ( copy x) => return x,
@@ -37,11 +67,18 @@ pub pure fn get<T: Copy>(opt: &Option<T>) -> T {
3767
3868pub pure fn get_ref < T > ( opt : & r/Option < T > ) -> & r /T {
3969 /*!
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.
4582 */
4683 match * opt {
4784 Some ( ref x) => x,
@@ -154,20 +191,37 @@ pub pure fn iter<T>(opt: &Option<T>, f: fn(x: &T)) {
154191#[ inline( always) ]
155192pub pure fn unwrap < T > ( opt : Option < T > ) -> T {
156193 /*!
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.
161208 */
162209 match move opt {
163210 Some ( move x) => move x,
164211 None => fail ~"option:: unwrap none"
165212 }
166213}
167214
168- /// The ubiquitous option dance.
169215#[ inline( always) ]
170216pub 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+ */
171225 if opt. is_none ( ) { fail ~"option:: swap_unwrap none" }
172226 unwrap ( util:: replace ( opt, None ) )
173227}
@@ -201,18 +255,38 @@ impl<T> &Option<T> {
201255 pure fn iter ( f : fn ( x : & T ) ) { iter ( self , f) }
202256 /// Maps a `some` value from one type to another by reference
203257 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+ */
205272 pure fn get_ref ( ) -> & self /T { get_ref ( self ) }
206273}
207274
208275impl < T : Copy > Option < T > {
209276 /**
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+ */
216290 pure fn get ( ) -> T { get ( & self ) }
217291 pure fn get_default ( def : T ) -> T { get_default ( & self , def) }
218292 /**
0 commit comments